Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There are two built in sorting operators in Perl - <=> and cmp - which are discussed in Equality Operators. <=> is used for numerical comparison and cmp is used for string comparison. Since the data from your first column are string data, you should use cmp.

If neither operator fits your requirements, you can design your own comparison function, following the instructions in sort.

On a side note, why are you using Text::CSV_XS if you are not using any of the code? Why are you both requiring and using the module when these are redundant operations? Why are you manually stripping leading zeros and why are you still feeding the numeric comparison operator values with leading quotes, even after I pointed out (Re^2: How do I sort a CSV file on multiple columns?) that it doesn't work the way you expect? Why are you posting code that has use strict commented out? See How do I post a question effectively?.

Update: I also just noticed that your @rows array contains strings, not array refs like your sort function expects. The line $result = join (",", @fields); concatenates all of the values in the array @fields into a single, comma-separated string. You then attempt to access each of those strings as if they were array refs, an issue that would have been caught by use strict;. To understand how to use references in Perl, read perlref, perlreftut and/or perllol. Compare your code to the code I've already referenced above to see a prototype of how you should be accessing these data structures.


In reply to Re: Sorting help by kennethk
in thread Sorting help 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 studying the Monastery: (7)
As of 2024-03-28 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found