On 07/07/2012 14:07, Chris Stinemetz wrote:
Hello list,
I have constructed an anonymous array with all the data I need.
I am having some difficulty in accessing and printing out the data the way
I want.
For the sake of not cluttering this thread too much I have uploaded the
anonymous array Data::Dumper output at github:gist
https://gist.github.com/3066287
The output I am trying to achieve is simply printing out each index 0 for
each unique occurrence of index 1 (name1-6).
For example: for name 5 in the anonymous array the output should be:
801 CDM 1, CCU 1, CE 3 2
812 CDM 1, CCU 1, CE 5 37
816 CDM 1, 2, CBR 3, 15MHz 12
817 CDM 1, 2, CBR 3, 15MHz 32
817 CDM 1, 2, CBR 1, 15MHz 4
831 CDM 1, 2, CBR 3, 15MHz 22
848 CDM 1, CCU 2, CE 1 8
873 CDM 1, CCU 1, CE 3 2
874 CDM 3, CCU 1, CE 5 18
886 ASMB 1 TXAMP 9 2
Cluster: Name5
_____________________________
302 CDM 1, 2, CBR 1, 15MHz 2
317 CDM 1, 2, CBR 2, 15MHz 17
317 ASMB 1 TXAMP 6 15
340 TFU 1 4
371 CDM 1, 2, CBR 3, 15MHz 1
400 TFU 2 1
517 TFU 1 2
543 TFU 2 2
Cluster: Name6
and print the rest of the instances just like the above.
Sorry if this isn't clear let me know if there are any questions.
Hi Chris
Suppose the variable $data contains the anonymous array in the gist. If
you need to you can dereference the array as a whole by using @$data.
The first block within itis at $data->[0], and that block's name is
at $data->[0][1] with the main data at $data->[0][0].
The first thing to do is to collect all the different names in the data,
and the best tool for this is a hash. A loop like this just increments
an element of the %names hash for each record and leaves the keys of the
hash to show all the unique names.
my %names;
for my $item (@$data) {
$names{$item->[1]}++;
}
Now we can sort those keys (for neatness) and loop over the array
looking for all the data with a matching name.
for my $name (sort keys %names) {
for my $item (@$data) {
print $item->[0] if $item->[1] eq $name;
}
print "\nCluster: $name\n\n";
}
Which prduces the output you wanted.
Note that the solution others have posted is better for very large
amounts of data: I have written it this way to make it clearer for you.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/