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

useless use of sort???

by drock (Beadle)
on Mar 22, 2005 at 02:05 UTC ( #441361=perlquestion: print w/replies, xml ) Need Help??

drock has asked for the wisdom of the Perl Monks concerning the following question:

why isn't my sort working??? Thanks perl people! : )
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); 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"; if (/^\d+/) { s/fre//,print MEMOUT +(split)[4], $, ;} if (/^\d+/) { s/id//,s/wa//,print CPOUT (sort {$a <=> $b} +(split)[16] +),$,;} ##--## Added sort after "print CPOUT" } #sub num { $a <=> $b } #my @sortlist = sort num $cpuout; close (FILE) or warn unable to close file $!"; close (MEMOUT) or warn unable to close file $!"; close (CPOUT) or warn unable to close file $!"; __DATA__ So everyone is saying I cannot embed a sort within a print of a FileHandle on column 16? I want to put the DATA below in n +umeric order. data from $cpuout 381305 377927 381305 377927 377165 373656 373614 373614 373248 390423 390423 390255 390111 389959

Replies are listed 'Best First'.
Re: useless use of sort???
by ysth (Canon) on Mar 22, 2005 at 02:15 UTC
    You don't appear to be using the @sortlist array; all your output seems to be already done at that point. What did you want to happen to the sorted data?

    It looks like perhaps you want to be saving things into a @cpout array instead of writing to CPOUT, and then sorting that and writing it after the <FILE> loop, but then again, maybe not.

Re: useless use of sort???
by chas (Priest) on Mar 22, 2005 at 03:01 UTC
    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.)
      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.
Re: useless use of sort???
by Anonymous Monk on Mar 22, 2005 at 09:39 UTC
    Strange looking code you have.
    • You are opening $vmout and $cpuout for write/read. But you never read (not that it would make much sense as you first truncate the file due to "+>").
    • What's with the $, = "\n", and then printing a scalar followed by $,. If you want to print two newlines, be explicite.
    • Why do you separate your substitutions and your prints with commas? Why not just semi-colons, like everyone else does?
    • (split)[16] is a one-element list. Perl will sort it for you, but that's not really going to change the order of the list.
    • You aren't checking the return values of your closes.
Re: useless use of sort???
by jdporter (Chancellor) on Mar 22, 2005 at 15:37 UTC
    It would really help if you would show some sample input, not just the (undesired) output you're getting.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://441361]
Approved by moot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2023-05-31 15:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found