Re: Sorting within a file
by sh1tn (Priest) on Mar 04, 2005 at 20:48 UTC
|
perl -e 'push @file,$_ while<>;print sort {$a <=> $b} @file' test.txt
Dos style:
perl -e "push @file,$_ while<>;print sort {$a <=> $b} @file" test.txt
Results:
1 b005824
134 b005376
402 astamen
972 achin00
181847 azeleke
1260896 abadeno
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
perl -e "print sort {$a <=> $b} <>" test.txt
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
UNIX style:
perl -e 'push @file,$_ while<>;print sort {$a <=> $b} @file' test.txt
Dos style:
perl -e "push @file,$_ while<>;print sort {$a <=> $b} @file" test.txt
Results:
1 b005824
134 b005376
402 astamen
972 achin00
181847 azeleke
1260896 abadeno
Is this on the commad line? How would I incorporate this into my scrip
+t? Thanks for the help.
| [reply] [d/l] |
|
|
Put this code in your program file:
use strict;
print sort {$a <=> $b} <>
__END__
Usage example: sort.pl data.txt
| [reply] [d/l] |
|
|
Surely "Unix-style" would be $ sort -n file.txt?
| [reply] [d/l] |
Re: Sorting within a file
by RazorbladeBidet (Friar) on Mar 04, 2005 at 20:26 UTC
|
| [reply] |
Re: Sorting within a file
by ikegami (Patriarch) on Mar 04, 2005 at 20:34 UTC
|
use strict;
use warnings;
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 { /^(\d+)/ && [ $_, 0+$1 ] }
# OR: map { no warnings; [ $_, 0+$1 ] }
@lines;
{
local *FILE;
open(FILE, "> filename.out")
or die("Couln't open output file: $!\n");
local $, = "\n";
print FILE (@sorted_lines);
}
Update: Fixed a warning. | [reply] [d/l] |
|
|
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... | [reply] [d/l] |
|
|
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 $,).
| [reply] [d/l] [select] |
|
|
|
|
|
|
Its still having problems. Error states "Can't use string ("") as an array ref while 'strict ref' is in use at line "map { /^(\d+)/ && $_, 0+$1 }"
| [reply] |
|
|
You must have a typo somewhere???
| [reply] |
Re: Sorting within a file
by gopalr (Priest) on Mar 05, 2005 at 04:53 UTC
|
while (<DATA>)
{
if ($_=~m#^([^\s]+)\s+([^\n]+)\n#)
{
push (@unordered, [$1,$2]);
}
}
@ordered = sort {$a->[0] <=> $b->[0]} @unordered;
print "\n$_->[0] = $_->[1]" for @ordered;
__DATA__
1260896 abadeno
972 achin00
402 astamen
181847 azeleke
134 b005376
1 b005824
| [reply] [d/l] |
Re: Sorting within a file
by dmorelli (Scribe) on Mar 04, 2005 at 20:53 UTC
|
#! /usr/bin/perl -w
use strict;
# Read whole file into a list, this could be an open()ed filehandle
# instead of the DATA section from below
my @lines = <DATA>;
# Sort the list by comparing the numeric portion of the lines
my @sorted = sort {
my ($numA) = $a =~ /^(\d+).*/;
my ($numB) = $b =~ /^(\d+).*/;
$numA <=> $numB
} @lines;
# Output the sorted list
{
# We never chomped, the lines still contain newlines,
# so don't need a list separator
local $" = "";
print "@sorted";
}
__DATA__
1260896 abadeno
972 achin00
402 astamen
181847 azeleke
134 b005376
1 b005824
| [reply] [d/l] |
Re: Sorting within a file
by trammell (Priest) on Mar 04, 2005 at 21:01 UTC
|
$ cat > data
1 b005824
1260896 abadeno
134 b005376
181847 azeleke
402 astamen
972 achin00
$ sort -n data
1 b005824
134 b005376
402 astamen
972 achin00
181847 azeleke
1260896 abadeno
| [reply] [d/l] |
Re: Sorting within a file
by Fletch (Bishop) on Mar 04, 2005 at 21:10 UTC
|
puts ARGF.sort_by{|l|l.split(/\s+/)[0].to_i}
| [reply] [d/l] |
Re: Sorting within a file
by Miguel (Friar) on Mar 05, 2005 at 21:06 UTC
|
Yet another way, using IO::All and unpack, as your Data looks fixed length delimited:
#!/usr/bin/perl -w
use strict;
use IO::All;
my @AoA;
push @AoA, [ unpack("A7 A8",$_) ] foreach io("file.txt")->getlines;
foreach ( sort { $a->[0] <=> $b->[0] } @AoA ) {
$_->[1] =~s/^ //;
print $_->[0], " ", $_->[1],"\n"
}
Miguel | [reply] [d/l] [select] |
Re: Sorting within a file
by kscaldef (Pilgrim) on Mar 06, 2005 at 07:53 UTC
|
| [reply] |
Re: Sorting within a file
by tphyahoo (Vicar) on Mar 07, 2005 at 10:41 UTC
|
| [reply] |