Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: How to Remove Commas ?

by allendevans (Initiate)
on Mar 27, 2012 at 13:05 UTC ( [id://961918]=note: print w/replies, xml ) Need Help??


in reply to Re: How to Remove Commas ?
in thread How to Remove Commas ?

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.

Replies are listed 'Best First'.
Re^3: How to Remove Commas ?
by johngg (Canon) on Mar 27, 2012 at 13:53 UTC

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

    Cheers,

    JohnGG

Re^3: How to Remove Commas ?
by tobyink (Canon) on Mar 27, 2012 at 14:06 UTC

    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://961918]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found