http://www.perlmonks.org?node_id=473468


in reply to How do i extract 3 variables from each line in a file, and print them to a new file

I believe this line should do it:

$_ =~ /(\d+)ms/g; print "$1,$2,$3\n";

UPDATE: I agree with comments since I've got the same result... Here's what worked:

$_ = "3434 34456... 321ms:543ms:45ms"; ($d, $e, $f) = ($_ =~ /(\d+)ms/g); print "$d,$e,$f\n";

--------------------------------
An idea is not responsible for the people who believe in it...

  • Comment on Re: How do i extract 3 variables from each line in a file, and print them to a new file
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How do i extract 3 variables from each line in a file, and print them to a new file
by fishbot_v2 (Chaplain) on Jul 08, 2005 at 16:30 UTC

    Capturing doesn't work like that. You are overwriting $1 for each match.

    Try something like

    my @nums = $_ =~ /(\d+)ms/g;
    if you want to capture all matches from a /g.

Re^2: How do i extract 3 variables from each line in a file, and print them to a new file
by davidrw (Prior) on Jul 08, 2005 at 16:22 UTC
    perl -le '$_="123ms456ms789ms"; $_ =~ /(\d+)ms/g; print "$1,$2,$3\n"; +'
    prints just 123,,