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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The issue with your posted code can be traced back to your not using Text::CSV to parse the data, which I find ironic given your node title (Update: previous title "Text::CSV"). With your splitting above, your data values are $x = '"001"', not the $x = '001' you seem to expect. If you had turned on warnings, you would have gotten Argument ""2"" isn't numeric in numeric comparison (<=>) repeatedly, since "2" is clearly not numeric. Text::CSV will automatically clean up the quotes, and thus your numbers would have been properly be interpreted as numbers. A la:
#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $sheet; my $count = -1; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attr +ibute. or die "Cannot use CSV: ".Text::CSV->error_diag (); my @rows = (); while ( my $row = $csv->getline( *DATA ) ) { push @rows, $row; } my $header = shift @rows; foreach my $row ( sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } +@rows ) { print join( ',', @$row ), "\n"; } __DATA__ Name,Score,State "001","67","CA" "2","67","CA" "12","63","FL" "1","72","IL" "1","32","AZ"

See Use strict warnings and diagnostics or die.


In reply to Re^2: How do I sort a CSV file on multiple columns? by kennethk
in thread How do I sort a CSV file on multiple columns? by mmittiga17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found