Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sorting one column of results

by choroba (Abbot)
on Nov 12, 2012 at 10:40 UTC ( #1003412=note: print w/ replies, xml ) Need Help??


in reply to Sorting one column of results

Instead of printing to OUT, push [@text] to an array. Then, sort the array by { $a->[10] cmp $b->[10] } and print map @$_ it.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Sorting one column of results
Select or Download Code
Re^2: Sorting one column of results
by perlnoobster (Sexton) on Nov 12, 2012 at 10:58 UTC
    hello choroba, Thank you for replying, I have adjusted the coding to the following but it still does not work?, please help?
    my $infile="orders.txt"; my $output2 = 'orders_today.txt'; open (OUT, "+>$output2"); open (FILE, $infile) or die "ERROR: File does not exist\n"; for (<FILE>) { my @text =(); @text = split /\t/,$_; chomp @text; push (@text, $text[0]); push (@text, $text[1]); push (@text,$text[2]); push (@text,$text[3]); { $a->[10] cmp $b->[10] } print map @$_ }
      I assumed you knew more about Perl. My fault. When using a function you do not know yet, consult its documentation.
      Here is what I had in mind:
      #!/usr/bin/perl use warnings; use strict; my $infile = 'orders.txt'; my $output = 'orders_today.txt'; open my $IN, '<', $infile or die "ERROR: Cannot open '$infile': $!"; my @out; while (<$IN>) { chomp; my @text = split /\t/; push @out, [@text]; } open my $OUT, '>', $output or die "Cannot open '$output': $!"; print {$OUT} map "@$_\n", sort { $a->[10] cmp $b->[10] } @out;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        hello choroba, Thank you for replying, the coding works however the output is all in one line, even though the split function is in the coding? not sure why? and is it possible to only print the following (as opposed to the entire original file?) print OUT "$text[10]\n$text[11]\nQuantity:$text[12]\n\n$text[16]\n$text[17]\n$text[18]\n$text[19]\n$text[20]\n$text[21]\n$text[22]\n$text[23]\n\n\n"; Thank you so much :)!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-07-12 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls