Dear Masters,
My question here is an extention to my earlier postings.
In particular to the answer given by frodo72.
I am extending the very first block of his code.
The intention is to include variations of the animal
during the counting. Such that given this:
my %bighash = (
'Arizona'=> {
'ZOO_1' => [ '5','HIPPO1', ['feat1'],['feat2']],
'ZOO_2' => [ '10','HIPPO2',['feat1'],['feat2']],
'ZOO_3' => [ '2', 'PUMA1', ['feat1'],['feat2']],
'ZOO_5' => [ '13', 'PUMA2',['feat1'],['feat2']],
},
'Indiana' => {
'ZOO_2' => [ '10','HIPPO3', ['feat1'],['feat2']],
'ZOO_9' => [ '25','ZEBRA1', ['feat1'],['feat2']],
'ZOO_5' => [ '13','MONKEY1', ['feat1'],['feat2']],
'ZOO_6' => [ '23','ZEBRA2', ['feat1'],['feat2']],
'ZOO_3' => [ '2', 'PUMA3', ['feat1'],['feat2']],
'ZOO_5' => [ '1', 'PUMA4', ['feat1'],['feat2']],
},
'Nevada' => {
'ZOO_3' => [ '3', 'HIPPO4', ['feat1'],['feat2']],
'ZOO_7' => [ '11', 'HIPPO5', ['feat1'],['feat2']],
'ZOO_4' => [ '21', 'LION1', ['feat1'],['feat2']],
'ZOO_12' => [ '13','MONKEY2',['feat1'],['feat2']],
},
);
My code below:
my %animals;
my @zoos;
foreach my $state (keys %bighash)
{
foreach my $zoo (keys %{$bighash{$state}})
{
foreach my $state2 (keys %bighash)
{
foreach my $zoo2 (keys %{$bighash{$state2}})
{
my $animal1 = $bighash{$state}{$zoo}[1];
my $animal2 = $bighash{$state2}{$zoo2}[1];
if ( is_variation($animal1,$animal2) == 1 )
{
#print "$animal1 - $animal2\n";
push @zoos, $zoo;
}
push @{$animals{$animal1}{$state}}, [@zoos];
}
}
}
}
print Dumper \%animals ;
sub is_variation
{
#Test if two animals are variation of others:
#e.g. HIPPO1 - HIPPO2 -> True
# HIPPO1 - MONKEY2 -> False
my ($s1,$s2) = @_;
my ($t1,$t2);
$s1 =~ /([A-Za-z]+)(\d+)$/;
$t1 = $1;
$s2 =~ /([A-Za-z]+)(\d+)$/;
$t2 = $1;
#print "$t1-$t2\n";
if ( $t1 eq $t2 )
{
return 1;
}
return 0;
}
Should've given this:
__END__
$VAR1 = {
'HIPPO1' => {
'Arizona'=> ['ZOO_1', 'ZOO_2'],
# from Zoo1_1 HIPPO1 and Zoo_2 HIPPO2
'Indiana'=> ['ZOO_2'],
'Nevada' => ['ZOO_3', 'ZOO_7'],
}
# So the hash above contain list of States and
# Zoos that own variations of Hippo (HIPPO1..5)
# Same thing apply for animals below:
'HIPPO2' => {
'Arizona'=> ['ZOO_1', 'ZOO_2'],
'Indiana'=> ['ZOO_2'],
'Nevada' => ['ZOO_3', 'ZOO_7'],
}
'HIPPO3' => {
'Arizona'=> ['ZOO_1', 'ZOO_2'],
'Indiana'=> ['ZOO_2'],
'Nevada' => ['ZOO_3', 'ZOO_7'],
}
'HIPPO4' => {
'Arizona'=> ['ZOO_1', 'ZOO_2'],
'Indiana'=> ['ZOO_2'],
'Nevada' => ['ZOO_3', 'ZOO_4'],
}
'HIPPO5' => {
'Arizona'=> ['ZOO_1', 'ZOO_2'],
'Indiana'=> ['ZOO_2'],
'Nevada' => ['ZOO_3', 'ZOO_4'],
}
'PUMA1' => {
'Arizona'=> ['ZOO_3', 'ZOO_5'],
'Indiana'=> ['ZOO_3', 'ZOO_5'],
}
'PUMA2' => {
'Arizona'=> ['ZOO_3', 'ZOO_5'],
'Indiana'=> ['ZOO_3', 'ZOO_5'],
}
'PUMA3' => {
'Arizona'=> ['ZOO_3', 'ZOO_5'],
'Indiana'=> ['ZOO_3', 'ZOO_5'],
}
'PUMA4' => {
'Arizona'=> ['ZOO_3', 'ZOO_5'],
'Indiana'=> ['ZOO_3', 'ZOO_5'],
}
'MONKEY1' => {
'Indiana'=> ['ZOO_5'],
'Nevada'=> ['ZOO_12'],
}
'MONKEY2' => {
'Indiana'=> ['ZOO_5'],
'Nevada'=> ['ZOO_12'],
}
'ZEBRA1' => {
'Indiana'=> ['ZOO_6','ZOO_9'],
}
'ZEBRA2' => {
'Indiana'=> ['ZOO_6','ZOO_9'],
}
'LION1' => {
'Nevada'=> ['ZOO_4'],
}
};
But it doesn't.
Is there anything I miss in my code there?
---
neversaint and everlastingly indebted.......
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|
|