Solved

Sorting - Help needed

Posted on 2006-11-28
8
212 Views
Last Modified: 2010-03-05
My perl program parses data and outputs the relevant values I need into an array.
(code has a series of ifels conditions I check).

push (@array1,$var1, ",", $var2, ",", $var3, ",", $var4, ",", $var5, ",", $var6,"\n");


The output.csv file contains 6 columns, namely, var1, var2,....var6.
I need to sort my output.csv file first by $var4 and then by $var5. I would appreciate if you could:
1) Tell me what are the ways this can be achieved.

2) Code that would do this (with a brief explanation)


0
Comment
Question by:nathan_007
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18029316
while( <> ){
my ($var1, $var2, $var3, $var4, $var5, $var6) = split/,/;
push @array,"$var4\0$var5\0$var1, $var2, $var3,  $var4, $var5, $var6\n";
}
@array = map{/.*\0(.*)/s}sort @array;
print @array;

0
 

Author Comment

by:nathan_007
ID: 18031286
Thanks. In most cases $var4 and $var5 are 5 digit integers. However I enocuntered an issue when $var5 happened to be a 4 digit number.

10012, 3068 appeared after 10012, 10078

3068 needs to be as 03068 in the array.

How do I format $var4 and $var5 it in the array to be of 5 digits in length.....something similar to sprintf"%05d" ????
0
 
LVL 5

Expert Comment

by:Alkali_Guy
ID: 18031423
$var4 = "0".$var4 if (length($var4)==4);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nathan_007
ID: 18031600
Alkali_Guy:
I agree that it works if the length is 4 digits. Is there a generic check I can do and round it to 5 digits assuming $var5 can be of length 1 or 2 or 3 or 4 or 5.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 50 total points
ID: 18032171
$var5 = sprintf("%05d", $var5);
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 18032196
push @array,sprintf"%6d\0%6d\0%s\n",$var4,$var5,"$var1, $var2, $var3,  $var4, $var5, $var6";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort hash by values desc 2 186
delete query using perl dbi 3 96
Perl DBI Transactions Using Custom Module 7 45
How to translate this 2-line while loop into C from Perl? 8 74
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now