Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Long list is long

by tybalt89 (Monsignor)
on Oct 30, 2022 at 14:28 UTC ( [id://11147844]=note: print w/replies, xml ) Need Help??


in reply to Long list is long

If I were on a linux system, I'd try it this way. The perl takes almost no memory :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11147822 use warnings; use Path::Tiny; path("/tmp/11147822.$_.txt")->spew( <<END ) for 1 .. 100; # FIXME for +testing only foo 73 bar 35 word 27 blah 23 END open my $fh, '-|', 'sort /tmp/11147822.*.txt' or die; # FIXME for your + file list open my $tosort, '|-', 'sort -n -k2' or die; <$fh> =~ /^(\w+)\s+(\d+)/ or die; my ($name, $value) = ($1, $2); while( <$fh> ) { if( /^(\w+)\s+(\d+)/ ) { if( $1 eq $name ) { $value += $2; } else { print $tosort "$name\t$value\n"; ($name, $value) = ($1, $2); } } } print $tosort "$name\t$value\n"; # be sure to print last one close $tosort or die;

Outputs:

blah 2300 word 2700 bar 3500 foo 7300

Replies are listed 'Best First'.
Re^2: Long list is long
by Chuma (Scribe) on Oct 30, 2022 at 15:18 UTC
    I have no idea what this does, but it looks cool! I'm using a mac, would that work? Supposing I have the file list in @files and the output file name in $outpath, how would you integrate that?

      I don't have a mac so I can't test this, but I assume the mac has a "sort" app somewhat like the *nix app (try "which sort" at a command line, and then "man sort"), then my guess is something like this:

      #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11147822 use warnings; my @files = map "/tmp/11147822.$_.txt", 1 .. 100; # FIXME my $outpath = "/tmp/11147822.outpath"; # FIXME open my $fh, '-|', "sort @files" or die; open my $tosort, '|-', "sort -n -k2 >$outpath" or die; <$fh> =~ /^(\w+)\s+(\d+)/ or die; my ($name, $value) = ($1, $2); while( <$fh> ) { if( /^(\w+)\s+(\d+)/ ) { if( $1 eq $name ) { $value += $2; } else { print $tosort "$name\t$value\n"; ($name, $value) = ($1, $2); } } } print $tosort "$name\t$value\n"; # be sure to print last one close $tosort or die;

      Assuming mac's are sort of built on BSD and still have a command line and have a more or less "sort" app.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 17:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found