in reply to Re: Sorting within a file
in thread Sorting within a file

my @lines; { local *FILE; open(FILE, "< filename.in") or die("Couln't open input file: $!\n"); chomp(@lines = <FILE>); } my @sorted_lines = map { $_->[0]} sort ( $a->[1] <=> $b->[1] } map { [ $_, 0+$_ ] } @lines; { local *FILE; open(FILE, "> filename.out") or die("Couln't open output file: $!\n"); local $, = "\n"; print FILE (@lines); } Whats with the curly brackets? Here's what I have tried. It looks like + the file is already ordered alphabetically by id when I used "du -ks + *" but I need it by size which is the numbers. Here's my code, it do +esnt seem to change the order. #Push data into an array die("Cannot open Log file to read from.") unless(open(TAKE, "<unix_list.txt")); while ($line = <TAKE>) { push(@list_all, $line); } close(TAKE); @list_all = sort { $a <=> $b } @list_all; die("Cannot open Log file to write to.") unless(open(NEW, ">unix_list.txt")); print NEW @lines; close(NEW);
Thanks again...

Replies are listed 'Best First'.
Re^3: Sorting within a file
by ikegami (Patriarch) on Mar 04, 2005 at 20:45 UTC
    Whats with the curly brackets?

    It provides a scope for the local *FILE and the local $,. I don't like variables hanging around too long, especially when they have destructors (like FILE) or have side-effects (like $,).

      ikegami, its complaining about argument "1260896 abadeno" isnt numeric in addition at the "map { $_, 0+$_ }" line...
        oops, I had tested it without warnings. I just modified it so it doesn't give the warning.