Mar 21, 2001 #1 nappaji Programmer Mar 21, 2001 76 US How do I eliminate spaces using PERL??. say , I have $_ = "bill gates " or $_ "bill michael gates". I want all the spaces eliminated and the o/p as "billgates" or "billmichaelgates" Thanks in advance.
How do I eliminate spaces using PERL??. say , I have $_ = "bill gates " or $_ "bill michael gates". I want all the spaces eliminated and the o/p as "billgates" or "billmichaelgates" Thanks in advance.
Mar 21, 2001 #2 jerehart Programmer Jun 10, 1999 61 US These should work $_ =~ s/\ //; or for all whitespace $_ =~ s/[\ \t\n]+//; Miah Upvote 0 Downvote
Mar 21, 2001 1 #3 tsdragon Programmer Dec 18, 2000 5,133 US There are a number of things wrong with the above examples: 1. If you are using $_ you don't need to specify it. 2. You don't need to escape the space in the pattern. 3. You should be using the '+' flag for "one or more" after the space, it's more efficient. 4. You should be using the 'g' flag for "global", otherwise it will only remove the FIRST space. 5. What's wrong with using the whitespace pattern '\s'? To remove just spaces (from $_) use: Code: s/ +//g; To remove all whitespace (from $_) use: Code: s/\s+//g; Meddle not in the affairs of dragons, For you are crunchy, and good with mustard. Upvote 0 Downvote
There are a number of things wrong with the above examples: 1. If you are using $_ you don't need to specify it. 2. You don't need to escape the space in the pattern. 3. You should be using the '+' flag for "one or more" after the space, it's more efficient. 4. You should be using the 'g' flag for "global", otherwise it will only remove the FIRST space. 5. What's wrong with using the whitespace pattern '\s'? To remove just spaces (from $_) use: Code: s/ +//g; To remove all whitespace (from $_) use: Code: s/\s+//g; Meddle not in the affairs of dragons, For you are crunchy, and good with mustard.