Hope you all had a good easter.
I have an interesting problem. I have a list of 38 values that need to be sorted. At this point quite simple but it gets worse.
The list is made up of pairs of values in the form 'Qx.1=zzz' and 'Qx.2=zzz' (where 'x' is a number from 1 to 20 and 'zzz' is a short string of variable length).
There are 19 items with 'Qx.1' and 19 with 'Qx.2'
Eg:
I sort the list with the x.1s and x.2s together:
The problem is there should be 20 pairs, not 19. I need to figure out which pair is missing and insert it at the right point.
For example if there is no Q19 I need to insert 'Q19.1=null' and 'Q19.2=null' into the list.
Here is a neat sort function I prepared earlier:
I plug into the sub an array that contains the data.
In the array, the pair association is correct but the pairs are out of order (see first example). Converting the array to a hash effectively takes care of the pair associations.
My sort works nicely but it doesn't detect the missing pair.
I was thinking of checking the 'x' number in each hash key against an incrementing counter in a loop, but that seems highly inefficient.
Any ideas?
Alchemy is easy with Perl!
s/lead/gold/g;
I have an interesting problem. I have a list of 38 values that need to be sorted. At this point quite simple but it gets worse.
The list is made up of pairs of values in the form 'Qx.1=zzz' and 'Qx.2=zzz' (where 'x' is a number from 1 to 20 and 'zzz' is a short string of variable length).
There are 19 items with 'Qx.1' and 19 with 'Qx.2'
Eg:
Code:
Q1.1=bla
Q1.2=qaz
Q3.1=meh
Q3.2=ehh
Q2.1=foo
Q2.2=mar
I sort the list with the x.1s and x.2s together:
Code:
Q1.1=bla
Q1.2=qaz
Q2.1=foo
Q2.2=mar
Q3.1=meh
Q3.2=ehh
The problem is there should be 20 pairs, not 19. I need to figure out which pair is missing and insert it at the right point.
For example if there is no Q19 I need to insert 'Q19.1=null' and 'Q19.2=null' into the list.
Here is a neat sort function I prepared earlier:
I plug into the sub an array that contains the data.
In the array, the pair association is correct but the pairs are out of order (see first example). Converting the array to a hash effectively takes care of the pair associations.
Code:
sub orderPartA(){
my %orderHash = @_;
return map {($orderHash{$_},$_)} sort {(split(/\.|Q/,$a))[1] <=> (split(/\.|Q/,$b))[1]} keys %orderHash;
}
My sort works nicely but it doesn't detect the missing pair.
I was thinking of checking the 'x' number in each hash key against an incrementing counter in a loop, but that seems highly inefficient.
Any ideas?
Alchemy is easy with Perl!
s/lead/gold/g;