Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Ok, lets give that "simple effective" sample code some data:

use strict; use warnings; my @contents = sort <DATA>; print @contents; __DATA__ At the moment, due to the small file size, sort time is unimportant. At some point, when I get to that stage, the sorted data will be reuse +d. I tried your code, and it seems quite simple, hence, efective. BUT, It runs with no errors, but nothing prints.

Prints:

At some point, when I get to that stage, the sorted data will be reuse +d. At the moment, due to the small file size, sort time is unimportant. BUT, It runs with no errors, but nothing prints. I tried your code, and it seems quite simple, hence, efective.

which is sorted on the whole line. It works, but ain't what you want. So lets add in some brain hurty code to sort by the "second column": :)

use strict; use warnings; my @contents = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, extractColumn (1, $_)] } <DATA>; print @contents; sub extractColumn { my ($columnIndex, $line) = @_; my ($key) = $line =~ /(?:[^,]*,){$columnIndex}([^,]*)/; return $key; } __DATA__ At the moment, due to the small file size, sort time is unimportant. At some point, when I get to that stage, the sorted data will be reuse +d. I tried your code, and it seems quite simple, hence, efective. BUT, It runs with no errors, but nothing prints.

Prints:

BUT, It runs with no errors, but nothing prints. I tried your code, and it seems quite simple, hence, efective. At the moment, due to the small file size, sort time is unimportant. At some point, when I get to that stage, the sorted data will be reuse +d.

However if you are dealing with csv (comma separated variable) data then you really want to be using a module such as Text::CSV to read the file. You may like to check out a few nodes that have asked the "sort CSV" question before (Super Search SoPW remember): Sorting a CSV file and sorting CSV files may help too.


DWIM is Perl's answer to Gödel

In reply to Re^3: Sorting files you Have read by GrandFather
in thread Sorting files you Have read by brusimm

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 goofing around in the Monastery: (5)
As of 2024-03-28 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found