| [reply] [Watch: Dir/Any] |
right now i'm trying to use this, but it doesn't seem to work..
my @data
while (my $line = <TEMPFILE>) {
my @elements = $line;
push @data, \@elements; # need to store as ref
}
@data = sort { $$a[2] cmp $$b[2] or $$a[1] <=> $$b[1] } @data or die
+ "can't sort";
foreach my $item (@data) {
print TEMPFILE @$item;
print TEMPFILE "\n";
}
Edit by tye | [reply] [Watch: Dir/Any] [d/l] |
Sidenote: can you please please do us a favour and read Writeup Formatting Tips. Your posts are just about unreadable as is. Specifically, at least learn about the <code> tag.
Remove the or die "Can't sort". The or forces scalar context on its left operand, which is not what you want.
Makeshifts last the longest.
| [reply] [Watch: Dir/Any] |
ok....got it...thanks a lot.....one last question...if i want to add more columns into my file, how would i change the sort routine? specifically, the map function....
| [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |
You don't need the map function, rchou2. You took care of everything by using the for() loop. However, you did not split the data into fields.
Here's one way to do what you are asking, that borrows most of the syntax preferences from your example:
#!/usr/bin/perl -w
use strict;
rename 'tempfile.txt', 'tempfile.txt~'; # save a backup
open TEMPFILE, "<tempfile.txt~" or die $!;
my @data;
my @newcolumn = qw(d
e
f);
my $incr = 0;
for my $line (<TEMPFILE>) {
my @elements = split(/\s+/g, $line); # split the line on all w
+hitespace
push @elements, $newcolumn[$incr++]; # add a new column
push @data, \@elements; # need to store as ref
}
close TEMPFILE;
@data = sort { $$a[2] cmp $$b[2] || $$b[1] <=> $$a[1] } @data # or d
+ie "can't sort";
$, = " " ; # separate output fields by a space
open OUTFILE, ">tempfile.txt" or die $!;
foreach my $item (@data){
print OUTFILE "@$item\n";
}
close OUTFILE;
__END__
mkmcconn
| [reply] [Watch: Dir/Any] [d/l] |
There is nothing obvious about what's going on there, rchou2. Although it solves your problem (I think), it's not a very good answer because it's hard to explain and to understand. That's nothing in my favor - I can't immediately think of the way I would have done it before learning this trick.
Read more about the Schwartzian Transform to understand it. Especially, try to understand why it changes everything to remove the square brackets as you did in your cut and paste (you need them, to create the temporary arrays that are being used for sorting).
mkmcconn
| [reply] [Watch: Dir/Any] |