Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: 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:16 UTC ( [id://473470]=note: print w/replies, xml ) Need Help??


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

you'll probably see a bunch of different ways, including split .. here's one pure regex way:
open OUTFILE, ">out.txt"; open INFILE, "in.txt"; while(<INFILE>){ next unless /(\d{1,3})ms:(\d{1,3})ms:(\d{1,3})ms/; print OUTFILE join(",", ($1, $2, $3) ), "\n"; } close OUTFILE; close INFILE;
  • 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 bofh_of_oz (Hermit) on Jul 08, 2005 at 16:22 UTC
    That one is an almost complete solution... Mine is shorter; this one should give the closest match... Add "$" at the end of your regex and it will be perfect...

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

      your solution didn't work ...

      Yeah, i guess i could anchor to the end (especially since OP said 'last 3 numbers' but might be able to assume that the ":ms" strings in there will be restrictive enought to get the right one.. i guess to go strictly by 'last 3 numbers' the (\d+)\D+ solution above is better (though in that one i would make the last \D+ a \D*)..
        I realized that and updated one works :)

        And in the solution above, last \D+ can stay \D+ instead of \D* because "ms" is two letters, and \D+ gets at least one so it also matches two but not matches zero occurrences (as opposed to \D*, which does)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found