Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: useless use of sort???

by chas (Priest)
on Mar 22, 2005 at 03:01 UTC ( [id://441374]=note: print w/replies, xml ) Need Help??


in reply to useless use of sort???

I don't see what you are trying to sort. In print CPOUT (sort {$a <=> $b} +(split)[16]) isn't (split)[16] just a single word from some line of the file? (sort acts on a list.)
chas

Replies are listed 'Best First'.
Re^2: useless use of sort???
by drock (Beadle) on Mar 22, 2005 at 14:52 UTC
    split16 is a column of data that I want to split which is illustrated under __DATA__. so you are saying I cannot embed a sort within a print of a Filehandle on column 16? thanks
      Sure you can, but that's not what your code does. If you want to sort column 16, you will first have to select the 16th column entirely, before you can sort it. So you would do:
      1. Open your input file.
      2. Read it, line by line. Extract column 16, push it to an array.
      3. Close your input file.
      4. Sort your array.
      5. Open your output file.
      6. Write your sorted array.
      7. Close your output file.
      What you certainly can't do is reading the input one line at a time, writing something for each line, and then expect the entire thing to be sorted when you're done.
        Understood... thank you! In steps 4,5,6 I am having syntax issues. Is the sorted array on the right? @unsotrted = sort {$b <=> $a } @sorted; Here is my code:
        #use diagnostics; use warnings; use strict; my $file = qq(/var/adm/sa/vm); my $vmout = qq(/var/adm/sa/vm.out); my $cpuout = qq(/var/adm/sa/cpu.out); my $art = qq(/var/adm/sa/arout); my @ar = (); my @ar2 = (); open (FILE, "<$file") || die "unable to open file $!"; open (MEMOUT, ">>$vmout") || die "unable to open file $!"; open (CPOUT, ">>$cpuout") || die "unable to open file $!"; while (<FILE>) { local $, = "\n"; s/id//, s/wa//, print CPOUT push @ar, +(split)[16], $,; s/id//, s/wa//, print MEMOUT +(split)[4], $,; } close (MEMOUT) or warn "unable to close file $!"; close (CPOUT) or warn "unable to close file $!"; close (FILE) or warn "unable to close file $!"; @ar = sort { $a <=> $b } @ar; print "Now printing array 1\n", @ar;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found