Of course the correct answer is "use Text::CSV":

#!/usr/bin/perl use strict; use warnings; use Text::CSV; use 5.016; my $csv = Text::CSV->new({binary => 1, sep_char => "\t"}); my $row; 1 while $row = $csv->getline(*DATA) and $row->[0] !~ /Invoice ID/; my @columns = @$row; my @data_lines; push @data_lines, $row while $row = $csv->getline(*DATA); # Sort data according to "Company Name" field then "Invoice ID" field. my ($company_name_index) = grep {$columns[$_] eq "Company Name"} (0 .. $#columns); my ($invoice_ID_index) = grep {$columns[$_] eq "Invoice ID"} (0 .. $#c +olumns); @data_lines = sort { fc($a->[$company_name_index]) cmp fc($b->[$company_name_index]) or $a->[$invoice_ID_index] <=> $a->[$invoice_ID_index] } @data_lines; # Print a new header and the column line print "Replacement Header Text Here\n\n", join ("\t", @columns), "\n"; # then print the sorted data print join ("\t", @$_), "\n" foreach @data_lines; __DATA__ Original Header Text Invoice ID Company Name 1 SEBASTIAN COMMUNICATIONS 2 MASQUE SOUND 3 SEALEVEL SYSTEMS 4 "MASSTECH, INC" 5 SE INTERNATIONAL 6 "SOUTHEAST SERVO, LLC" 7 "SEALEVEL SYSTEMS, INC." 8 MASTERBILT

Prints:

Replacement Header Text Here Invoice ID Company Name 2 MASQUE SOUND 4 MASSTECH, INC 8 MASTERBILT 5 SE INTERNATIONAL 3 SEALEVEL SYSTEMS 7 SEALEVEL SYSTEMS, INC. 1 SEBASTIAN COMMUNICATIONS 6 SOUTHEAST SERVO, LLC

Update: and of course Text::CSV should be used to correctly output the data too:

# Print a new header and the column line to it print "Replacement Header Text Here\n\n"; # print the sorted data. print render($csv, @columns), "\n"; print render($csv, @$_), "\n" foreach @data_lines; sub render { my ($csv, @cells) = @_; $csv->combine(@cells); return $csv->string(); }

with modiffied output:

Replacement Header Text Here "Invoice ID" "Company Name" 2 "MASQUE SOUND" 4 "MASSTECH, INC" 8 MASTERBILT 5 "SE INTERNATIONAL" 3 "SEALEVEL SYSTEMS" 7 "SEALEVEL SYSTEMS, INC." 1 "SEBASTIAN COMMUNICATIONS" 6 "SOUTHEAST SERVO, LLC"
Premature optimization is the root of all job security

In reply to Re: Sorting an array of strings when some of the strings have commas in them? by GrandFather
in thread Sorting an array of strings when some of the strings have commas in them? by perldigious

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.