Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to Remove Commas ?

by ramlight (Friar)
on Mar 27, 2012 at 12:22 UTC ( #961913=note: print w/replies, xml ) Need Help??


in reply to How to Remove Commas ?

The substitute command works really nicely here:
# remove commas from $filesize string # =================================== $filesize =~ s/,//;
Regular expressions like these are one of Perl's strengths.

Replies are listed 'Best First'.
Re^2: How to Remove Commas ?
by allendevans (Initiate) on Mar 27, 2012 at 13:05 UTC

    ramlight,

    Thanks for your post! I'm having the best success with your suggestion, some others caused perl to crash. :(

    I pasted the snippet into my script ...

    # while not eof # ============= while ($line = <INPUT>) { ($date, $time, $ampm, $filesize, $filename) = split(" ", $line); # remove commas from $filesize string # =================================== $filesize =~ s/,//;

    and it successfully removed the first comma. (below)

    Results from gnome-terminal:

    1572,727,014 IN11-135.E01 1223 IN11-135.E01.txt 1572,694,696 IN11-135.E02 1572,740,428 IN11-135.E03 1572,785,002 IN11-135.E04 1572,696,748 IN11-135.E05 1572,745,871 IN11-135.E06 1572,737,726 IN11-135.E07 1572,785,459 IN11-135.E08 1572,777,135 IN11-135.E09 1572,751,922 IN11-135.E10 1572,684,462 IN11-135.E11 1556,456,660 IN11-135.E12 Total Files: 13 Avg Size: 0

    The first comma was removed from each number. Now I'm off to perldoc.perl.org to see how I can remove the remaining commas.

    Thanks ... Allen.

      Have another read of the answer roboticus gave you, paying particular attention to the 'g' flag of the substitution.

      Cheers,

      JohnGG

      The first comma was removed from each number.

      The s/// operator by default removes only the first match. So:

      my $var = "foobarfoobaz"; $var =~ s/foo//; say $var; # says "barfoobaz"

      There are various flags you can include to alter its default behaviour though. One of the most useful is the "g" (global) flag...

      my $var = "foobarfoobaz"; $var =~ s/foo//g; say $var; # says "barbaz"

      Note that the slashes may be replaced with other characters, so you could equally write:

      my $var = "foobarfoobaz"; $var =~ s@foo@@g; say $var; # says "barbaz"

      Or even:

      my $var = "foobarfoobaz"; $var =~ s{foo}{}g; say $var; # says "barbaz"

      ... which some people might find more readable. Though note that there are a handful of characters (hash, question mark and single quote spring to mind) that trigger special behaviours here (perlop has more details).

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://961913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2022-08-08 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?