Using chop / chomp to remove spaces and tabs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perl_begi
    New Member
    • Mar 2006
    • 1

    Using chop / chomp to remove spaces and tabs

    I am having a file that contains different strings, which in turn are name of folders.

    For example test. file has:
    test0002
    intel/drivers/

    Here there can be spaces, tabs after folder names (like we can place space after test0002 or it may happen when you edit file in notepad and use same in Unix test0002 becomes test0002^M).

    I used chop and chomp to do it. What I did was I read all contents in array then I did chop (or chomp) for each element of array but nothing works. I m unable to remove extra spaces, tabs.

    How can this be done to get desired result?
    Last edited by eWish; Nov 18 '07, 02:24 AM. Reason: Fixed Format, Fixed Grammer and Edited Title
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Chomp function.
    Chop function.

    You could use a regex to remove the spaces or tabs.
    [CODE=perl]my $test =~ s/\n//g; #removes newline
    $test = s/\t//g; #removes tab

    #Remove Leading and Trailing Whitespace
    my $test =~ s/^\s+//;
    $test =~ s/\s+$//;[/CODE]

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by perl_begi

      I used chop and chomp to do it. What I did was I read all contents in array then I did chop (or chomp) for each element of array but nothing works. I m unable to remove extra spaces, tabs.

      How can this be done to get desired result?
      chop removes the last character from a string/line, chomp removes the input record seperator (usually a newline) from a string/line. Not sure why you thought either of those functions was appropriate for what you are doing. See Kevins post above.

      Comment

      Working...