J
joemacbusiness
Hi All,
I want a subroutine that will sort a file on any given column.
So I have a file like this:
300 400 500 600 700
33 2337483 482 78374 4567
10 20 30 40 50
1000 1001 1002 1003 1004
9 8 7 6 5
And I run my numericSortCol() routine on it sorting on
column 1 and should get this:
9 8 7 6 5
10 20 30 40 50
300 400 500 600 700
1000 1001 1002 1003 1004
33 2337483 482 78374 4567
The problem is that I cannot get the bynum() to accept 2 args.
The code will work if I hard-code the $col in bynum, and
tweek the "problem line" a bit but that defeats the purpose of the
"sort on column" feature.
Does anyone have a solution for this?
Thanks, --Joe M.
######################### Here's the code I have so far:
[joe@localhost work]$ cat test18.pl
#!/usr/bin/perl
my $infile = "test18.in";
open(F,"$infile") || print "cannot open $infile $!";
my @array = <F>;
close(F);
numericSortCol(1,\@array);
sub numericSortCol {
my $col = shift;
my $aref = shift;
foreach my $item (sort bynum($col,@{ $aref })){ # <<<< problem
line??
print "item: $item\n";
}
}
sub bynum {
my $col = shift;
@a = split(/\s+/,$a);
@b = split(/\s+/,$b);
$a[$col] <=> $b[$col];
# $a[1] <=> $b[1];
}
[joe@localhost work]$
[joe@localhost work]$
######################### Here's the input file
[joe@localhost work]$ cat test18.in
300 400 500 600 700
33 2337483 482 78374 4567
10 20 30 40 50
1000 1001 1002 1003 1004
9 8 7 6 5
[joe@localhost work]$
[joe@localhost work]$
######################### Here's the runtime:
[joe@localhost work]$ perl test18.pl
item: 1
item: 9 8 7 6 5
item: 10 20 30 40 50
item: 33 2337483 482 78374 4567
item: 300 400 500 600 700
item: 1000 1001 1002 1003 1004
[joe@localhost work]$
I want a subroutine that will sort a file on any given column.
So I have a file like this:
300 400 500 600 700
33 2337483 482 78374 4567
10 20 30 40 50
1000 1001 1002 1003 1004
9 8 7 6 5
And I run my numericSortCol() routine on it sorting on
column 1 and should get this:
9 8 7 6 5
10 20 30 40 50
300 400 500 600 700
1000 1001 1002 1003 1004
33 2337483 482 78374 4567
The problem is that I cannot get the bynum() to accept 2 args.
The code will work if I hard-code the $col in bynum, and
tweek the "problem line" a bit but that defeats the purpose of the
"sort on column" feature.
Does anyone have a solution for this?
Thanks, --Joe M.
######################### Here's the code I have so far:
[joe@localhost work]$ cat test18.pl
#!/usr/bin/perl
my $infile = "test18.in";
open(F,"$infile") || print "cannot open $infile $!";
my @array = <F>;
close(F);
numericSortCol(1,\@array);
sub numericSortCol {
my $col = shift;
my $aref = shift;
foreach my $item (sort bynum($col,@{ $aref })){ # <<<< problem
line??
print "item: $item\n";
}
}
sub bynum {
my $col = shift;
@a = split(/\s+/,$a);
@b = split(/\s+/,$b);
$a[$col] <=> $b[$col];
# $a[1] <=> $b[1];
}
[joe@localhost work]$
[joe@localhost work]$
######################### Here's the input file
[joe@localhost work]$ cat test18.in
300 400 500 600 700
33 2337483 482 78374 4567
10 20 30 40 50
1000 1001 1002 1003 1004
9 8 7 6 5
[joe@localhost work]$
[joe@localhost work]$
######################### Here's the runtime:
[joe@localhost work]$ perl test18.pl
item: 1
item: 9 8 7 6 5
item: 10 20 30 40 50
item: 33 2337483 482 78374 4567
item: 300 400 500 600 700
item: 1000 1001 1002 1003 1004
[joe@localhost work]$