in reply to Re: File content sorting based on type
in thread File content sorting based on type
Your
my @lines; while (<DATA>) { push @lines,$_; }
could more simply be written as
my @lines = <DATA>;
Rather than reading the file into an array, sorting it using a routine then printing it out, you could sort and print directly using a GRT.
$ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; 912828Q86 Note 1-Year 2016-05-25 2016-05-27 2018-0 +4-30 100.003850 NULL 912796HD4 Bill 4-Week 2016-01-26 2016-01-28 2016-0 +2-25 99.977056 NULL 912810RS9 Bond 30-Year 2016-05-12 2016-05-16 2046-0 +5-15 97.619462 2.500000 912810RQ3 Bond 29-Year 2016-04-14 2016-04-15 2046-0 +2-15 98.011430 2.500000 912796HD4 Bill 4-Week 2016-01-26 2016-01-28 2016-0 +2-25 99.977056 NULL 912828N71 Note 9-Year 2016-05-19 2016-05-31 2026-0 +1-15 103.533587 0.625000 EOD print for map { unpack q{x4a*}, $_ } sort map { pack q{a4a*}, ( split )[ 1 ], $_ } <$inFH>;' 912796HD4 Bill 4-Week 2016-01-26 2016-01-28 2016-0 +2-25 99.977056 NULL 912796HD4 Bill 4-Week 2016-01-26 2016-01-28 2016-0 +2-25 99.977056 NULL 912810RQ3 Bond 29-Year 2016-04-14 2016-04-15 2046-0 +2-15 98.011430 2.500000 912810RS9 Bond 30-Year 2016-05-12 2016-05-16 2046-0 +5-15 97.619462 2.500000 912828N71 Note 9-Year 2016-05-19 2016-05-31 2026-0 +1-15 103.533587 0.625000 912828Q86 Note 1-Year 2016-05-25 2016-05-27 2018-0 +4-30 100.003850 NULL $
Note that this code is a little naive in that it relies on the words in the sorting column all being the same length. More robust would be to use unpack, or perhaps substr, rather than split to extract the entire column for sorting including any trailing spaces so that the code would cope if "Invoice" cropped up.
I hope this is of interest.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: File content sorting based on type
by Marshall (Canon) on Jun 01, 2016 at 17:25 UTC |