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

Sorting Question

by lisaw (Beadle)
on Oct 04, 2004 at 18:28 UTC ( #396321=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm looking for support regarding alphabetically sorting data with the following CGI code:
$editlist = $FORM{'list'}; open(FILE, "$datadir/$editlist\.txt") || &error_message("Can't find d +ata file - $datadir/$editlist\.txt."); @list = <FILE>; close(FILE); $numlist = @list; $ccc = 0; for ($a = 0; $a < $numlist; $a ++) { ($two, $one, $nochop) = split(/,/, $list[$a]); if ($one =~ /.*\@.*\..*/) { print "$two,$one\n"; $ccc++; } }
I sincerely appreciate any help that anyone can provide! Lis

Replies are listed 'Best First'.
Re: Sorting Question
by allolex (Curate) on Oct 04, 2004 at 19:08 UTC

    In addition to the lovely sorting advice found here, you might consider rewriting your code in a more perlish way. The C-style loops with counters are all right, but perl has while and for(each) loops that do the same thing, except better. ;)

    --
    Damon Allen Davison
    http://www.allolex.net

Re: Sorting Question
by JediWizard (Deacon) on Oct 04, 2004 at 18:48 UTC

    I don't see any sorting going on in this code. What exactly are you trying to sort? If you are trying to sort @list, try this:

    @list = sort({$a cmp $b} @list);
    May the Force be with you
      Which is just the same as @list = sort @list

      Caution: Contents may have been coded under pressure.
        although you may want to actually sort alphabetically (ie case-insensitive) instead of ASCIIbetically:
        @list = sort { lc $a cmp lc $b } @list;
Re: Sorting Question
by Eimi Metamorphoumai (Deacon) on Oct 04, 2004 at 18:56 UTC
    Well, the code provided does no sorting. What is it you want it to do?

    If you want it to basically do what it's doing, but return the output in sorted order, this should work.

    my $editlist = $FORM{'list'}; open(FILE, "$datadir/$editlist.txt") or error_message("Can't find data file - $datadir/$editlist.txt."); my $ccc = 0; my @out; while (<FILE>){ my ($two, $one, $nochop) = split(/,/); if ($one =~ /.*\@.*\..*/) { push @out, "$two,$one\n"; $ccc++; } } close(FILE); print sort @out;
    Otherwise, you'll have to be a bit clearer about what you're expecting. I made a few incidental changes, reading the file in one line at a time instead of all at once, etc, but they're just for style. The heart of it is, instead of outputting one at a time, push them into an array you can sort and output. If that's what you want to do.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://396321]
Approved by CloneArmyCommander
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-07-07 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?