Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Sorting script deletes first line of data

by glemley8 (Acolyte)
on Aug 28, 2012 at 17:07 UTC ( #990285=perlquestion: print w/replies, xml ) Need Help??

glemley8 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that is meant to sort data by a certain column. It sorts the data, but the output is absent of the first line of data in the source file! Can anyone tell me what's wrong here? Thanks!
open (SOURCE, "<src_data.csv") or die "Can't open file"; open (OUTPUT, ">output_data.csv") or die "Can't create load file"; my $sheet; my $count = -1; my $csv = Text::CSV->new ({binary=>1}) or die "Cannot use CSV: ".Text: +:CSV->error_diag (); my @rows = (); while (my $row = $csv->getline(*SOURCE)){ push @rows, $row; } my $header = shift @rows; foreach my $row (sort {$a->[1] cmp $b->[1]} @rows){ print OUTPUT join(',', @$row), "\n"; }

Replies are listed 'Best First'.
Re: Sorting script deletes first line of data
by philiprbrenan (Monk) on Aug 28, 2012 at 17:18 UTC

    You have moved the first row to $header with shift()

      Wow, I feel silly. Sorry- this wasn't a script I wrote, and as you can tell, I'm just learning Perl. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://990285]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-12-01 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (15 votes). Check out past polls.

    Notices?