Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Transient (Hermit)
on Jul 08, 2005 at 16:56 UTC ( [id://473498]=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

TMTOWTDI - and it's usually wrong silly...
#!/usr/bin/perl use warnings; use strict; open( OUTPUT, ">output" ) or die "Unable to open output\n$!\n"; $,=","; $\="\n"; while (<DATA>) { print OUTPUT map { $_ =~ s/\D//g; $_ } split(':',(split(' ',$_))[-1]) +; } close OUTPUT or die "Close output failed\n$!\n"; __DATA__ 3434 34456 7788 9999 65444 4 444 444 44443225 12ms:233ms:455ms 755655 5789 333 666776 5553 353534 33 321ms:543ms:45ms
Update: ...and the finale:

perl -pe '$_=join(",",map{s/\D//g;$_} split(":",(split(" ",$_))[-1])). +"\n"' < input > output

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 sapnac (Beadle) on Jul 08, 2005 at 17:16 UTC
    Check this Data.dat contains the input lines. This is working. open(Spooler, "Data.dat") or die "File does not exists\n"; while($spooler=<Spooler>) { #print "$spooler\n"; chomp($spooler); @matches = $spooler =~ /(\d+)ms/g; foreach $a (@matches){ print $a .","; } print "\n"; } Hope it helps!
      That would print a comma at the end of the output string, so you would need a way to prevent it, let's say, like this:

      for (@matches) { print "$_,"; } print "\b ";

      (Sorry about replacing foreach with for - someone mentioned to me earlier that it's faster and works the same way anyways)

      Personally, I prefer Transient's way of doing things... although I don't quite understand why it parses split first and then map...

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

        good question.. hope I'm not overstepping my bounds by responding..(and I hope I'm right!)

        map { $_ =~ s/\D//g; $_ } split(':',(split(' ',$_))[-1]);
        is equivalent to
        my @split_results = split(':',(split(' ',$_))[-1]); map { $_ =~ s/\D//g; $_ } @split_results;
        because map BLOCK LIST has to run the BLOCK for each LIST element (setting each LIST value to $_), it must know what is in that LIST. Hence, the execution of the split(s) before the execution of the BLOCK (and therefore the map).

        for is faster to type than foreach, and makes your program 3 bytes smaller, but apart from that, it has no effect.



        pbeckingham - typist, perishable vertebrate.
      instead of
      @matches = $spooler =~ /(\d+)ms/g; foreach $a (@matches){ print $a .","; } print "\n";
      how about
      print join(',',$spooler=~/(\d+)ms/g)."\n";
      Thanks, that worked great:)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found