Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: substring/regex question

by rjt (Deacon)
on Aug 06, 2013 at 05:37 UTC ( #1048047=note: print w/replies, xml ) Need Help??


in reply to substring/regex question

Update: I may have misread your question. I missed the one little word in your description that changes the whole premise: "sort". I now believe you actually want to print the input file sorted on the 3rd column, not just print the 3rd column. I'd use the Schwartzian Transform for a pure-Perl solution:

print for map { pop @$_ } sort { $a->[0] <=> $b->[0] } map { [ (split /,/)[3], $_ ] } <>;

Of course, the following non-Perl solution works just as well on your UNIX-ish OS:

sort -t, -k4 -n <trace.csv >rgextract.txt
#!/opt/local/bin/perl -w

It appears you're on OS X or other UNIX-ish OS, so given this, and the apparent regularity of your data, I'd probably use the following one-liner:

perl -F, -anE 'say $F[3]' <trace.csv >rgextract.txt

See perlrun for information on the switches used, but basically:

-F, Autosplit delimiter is ',' -a Enable autosplit fields into @F -n Assume while (<>) { ... } around program -E Eval one-line program with optional features (i.e., 'say', in this case)
use strict; use warnings; omitted for brevity.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1048047]
help
Chatterbox?
[robby_dobby]: Likewise! I haven't seen a lot of people with HAs back where I come from, so I figured it might be the same around here too...
[robby_dobby]: Oh, shit -- I was replying to someone, but it wound up here. -_-
robby_dobby is still not used to PM's interface. /o\
[oiskuu]: War. War never changes.
[robby_dobby]: "My cat is a very good mouser" -- that's a great way to identify a Brit among a crowd :-)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-11-17 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (272 votes). Check out past polls.

    Notices?