Perdeep,
I think this code should do what you are looking for:
# retool your data into something more usable, a hash of arrays
foreach $key (keys %Values) {
# split the values for the key
@values = split(/\s/, $Values{$key});
foreach $val (@values) {
# set this value in an array at it's own index
${$parsedData{$key}}[$val] = $val;
# set the high odd number
if ($val % 2 == 1 && $val > $hiOdd) {
$hiOdd = $val;
}
# set the high even number
if ($val % 2 == 0 && $val > $hiEven) {
$hiEven = $val;
}
}
}
# output the reworded data
foreach $key (sort keys %parsedData) {
print "$key ";
# print out the odd values
for ($i=1; $i<=$hiOdd; $i+=2) {
if (defined ${$parsedData{$key}}[$i]) {
print " ${$parsedData{$key}}[$i],";
} else {
print " 0,";
}
}
print " ";
# print out the even values
for ($i=2; $i<=$hiEven; $i+=2) {
if (defined ${$parsedData{$key}}[$i]) {
print " ${$parsedData{$key}}[$i],";
} else {
print " 0,";
}
}
print "\n";
}
Matt
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Mehta, Perdeep
Sent: Monday, April 09, 2007 8:40 AM
To: [email protected]
Subject: [Perl-unix-users] printing hash values in a special order
Hi,
I have a hash with even numbers followed by odd numbers as values that I
want to print in a certain order. I have tried my self several hours but
couldn't work this out. Here is an example of input data. I will
appreciate any help.
%Values has following fields,
name(Key) bands(Values)
143B 08
2A5E 06 10
2AAA 10 12 07 09
2ABA 02 06 13
ABCA 07
ABC8 02
ABC2 10 11
ABCB 10
ABC1 08 10 07 09
ABC2 01 03
ABC5 06 01
ACH3 06
ACHA 04 08 01 05 09 11
ACLA 10
ACM4 03
ACTA 10 13
ACTB 02 06 08 20 01 03 07 11 13
ACTY 02 09 11
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The output should look like this.
All odd numbers first followed by even numbers and wherever a value is
missing print 0(zero). For example,
Name Odd bands Even bands
01, 03, 05, 07, 09, 11, 13, 02, 04, 06, 08, 10, 12
==== ========================== ======================
143B 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 08, 0, 0
2A5E 0, 0, 0, 0, 0, 0, 0, 0, 0, 06, 0, 10, 0
2AAA 0, 0, 0, 07, 09, 0, 0, 0, 0, 0, 0, 10, 12
2ABA 0, 0, 0, 0, 0, 0, 13, 02, 0, 06, 0, 0, 0
ABCA 0, 0, 0, 07, 0, 0, 0, 0, 0, 0, 0, 0, 0
ABCB 0, 0, 0, 0, 0, 0, 0, 02, 0, 0, 0, 0, 0
ABCC 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 10, 0
.
.
And so on ....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note: Top three lines are to make the output example clearer.
Thanks,
perdeep
Perdeep Mehta
Hartwell Center for Bioinformatics & Biotechnology
St. Jude Children's Research Hospital
Memphis, TN 38105
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs