Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Sorting an array of strings when some of the strings have commas in them?

by GrandFather (Saint)
on Dec 11, 2015 at 22:27 UTC ( [id://1150076]=note: print w/replies, xml ) Need Help??


in reply to Sorting an array of strings when some of the strings have commas in them?

If I turn your code into something to exercise your sort the first thing I find is that fc() is missing. Removing that the sort performs as expected:

#!/usr/bin/perl use strict; use warnings; chomp (my @data_lines = <DATA>); # Sort the data lines according to the "Company Name" field and then t +he "Invoice ID" field. @data_lines = sort { my ($company_name_a, $invoice_ID_a) = (split /~/, $a)[0, 1]; my ($company_name_b, $invoice_ID_b) = (split /~/, $b)[0, 1]; ($company_name_a) cmp ($company_name_b) or $invoice_ID_a <=> $invoice_ID_b } @data_lines; print "$_\n" for @data_lines; __DATA__ SEALEVEL SYSTEMS~1 SEALEVEL SYSTEMS, INC.~2 SEBASTIAN COMMUNICATIONS~3 MASQUE SOUND~4 MASSTECH, INC~5 MASTERBILT~6 SE INTERNATIONAL~7

Prints:

MASQUE SOUND~4 MASSTECH, INC~5 MASTERBILT~6 SE INTERNATIONAL~7 SEALEVEL SYSTEMS~1 SEALEVEL SYSTEMS, INC.~2 SEBASTIAN COMMUNICATIONS~3

You will notice that I replaced tabs with ~ to make the column separators easier to see.

So maybe you'd like to update my sample code to show your actual problem?

Update: ah, I see fc is "new" as of about Perl 5.16 and I hadn't "turned it on" even though using Perl 5.20 :(. Restoring fc to the sort doesn't alter the result.

Premature optimization is the root of all job security

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found