ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Got a bit of a weird one here, and I can't seem to work it out :(

The code is:
if (length $link->{LatitudLongitud} > 5) { if (!$seen->{$link->{LatitudLongitud}}) { push @{$GoogleMaps::records->{$link->{LatitudLongitud} +}}, $link; $seen->{$link->{LatitudLongitud}} = 1; # make sure we +set this, so we know its been seen already! } else { push @{$GoogleMaps::records->{$link->{LatitudLongitud} +}}, $link; } } if (length $link->{LatitudLongitud02} > 5 && $link->{LatitudLo +ngitud} ne $link->{LatitudLongitud02}) { if (!$seen->{$link->{LatitudLongitud02}}) { push @{$GoogleMaps::records->{$link->{LatitudLongitud0 +2}}}, $link; $seen->{$link->{LatitudLongitud02}} = 1; # make sure w +e set this, so we know its been seen already! } else { push @{$GoogleMaps::records->{$link->{LatitudLongitud0 +2}}}, $link; } }
This should then provide a list of the long/lat values found in the data, and then have an "array" for each of them (even if there is 1 item, or more than 1 with the same long lat values)

However - I was getting an error when trying to pass the array back into a loop, and after looking at Data::Dumper, I saw this:
'13.709931,-89.202365' => [ $VAR1->{'13.704011,-89.247029'}[ +0] ], '13.687479,-89.221301' => [ { 'isPopular' => 'No', 'Curri_Desc18' => '', 'ExpiryNotify' => '0' } ], '13.699487,-89.231086' => [ { 'isPopular' => 'No', 'Curri_Desc18' => '', 'ExpiryNotify' => '0' } ],


Notice the 2nd line:

$VAR1->{'13.704011,-89.247029'}[0]

I can't for the life of me work out thats going on:/

If I change the code to a simple string that gets passed in - then it works as expected (but obviously I don't have the values I need :():
if (length $link->{LatitudLongitud02} > 5 && $link->{LatitudLo +ngitud} ne $link->{LatitudLongitud02}) { if (!$seen->{$link->{LatitudLongitud02}}) { push @{$GoogleMaps::records->{$link->{LatitudLongitud0 +2}}}, "FOO"; $seen->{$link->{LatitudLongitud02}} = 1; # make sure w +e set this, so we know its been seen already! } else { push @{$GoogleMaps::records->{$link->{LatitudLongitud0 +2}}}, "FOO"; } }
Anyone got any suggestions as to whats going on here? I'm at a dead end, and have been trying to work this out for ages now (trying different approaches, etc) ... but nothing seems to be working

TIA!

Andy

Replies are listed 'Best First'.
Re: Weirdness with pushing records, and values saved
by roboticus (Chancellor) on Feb 13, 2011 at 19:02 UTC

    ultranerds:

    Ok, what's the problem with it? The $VAR1->{'13.704011,-89.247029'}[0] is (if I understand correctly) simply Data::Dumper's way of telling you that the first item in the array referenced by $VAR1->{'13.709931,-89.202365'} is the same item as the first item in the array referenced by $VAR1->{'13.704011,-89.247029'}. For example:

    $ cat 887880.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $t1 = {a=>1, b=>2, c=>3}; my $t2 = {a=>2, b=>5, c=>7}; my %H = ( X=>$t1, Y=>$t1, Z=>$t2 ); $H{Y}{b}=11; print Dumper(\%H); $ perl 887880.pl $VAR1 = { 'Z' => { 'c' => 7, 'a' => 2, 'b' => 5 }, 'X' => { 'c' => 3, 'a' => 1, 'b' => 11 }, 'Y' => $VAR1->{'X'} }; $

    If that's intended, then everything is fine. If it's not intended, then perhaps you're building $link incorrectly and re-using a hash, and overwriting your data as shown above. I can't see how you're building $link, nor how you're accessing the data, so I can't tell quite what your problem is.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Hi,

      aaah ok - that makes sense - not sure why its doing that though =) I ended up using a slight variation of the code now:
      if (length $link->{LatitudLongitud} > 5) { if (!$seen->{$link->{LatitudLongitud}}) { push @{$records->{$link->{LatitudLongitud}}}, $link->{ +ID}; $seen->{$link->{LatitudLongitud}} = 1; # make sure we +set this, so we know its been seen already! } else { push @{$records->{$link->{LatitudLongitud}}}, $link->{ +ID}; } } if (length $link->{LatitudLongitud02} > 5 && $link->{LatitudLo +ngitud} ne $link->{LatitudLongitud02}) { if (!$seen->{$link->{LatitudLongitud02}}) { push @{$records->{$link->{LatitudLongitud02}}}, $link- +>{ID}; $seen->{$link->{LatitudLongitud02}} = 1; # make sure w +e set this, so we know its been seen already! } else { push @{$records->{$link->{LatitudLongitud02}}}, $link- +>{ID}; } } $links->{$link->{ID}} = $link;
      ..and then after that, looping through it with this:
      map { my ($lat,$long) = split ",", $_; my @links = @{$records->{$_}}; my $box_contents; my @html_contents; my $num_links = $#links+1; foreach (@links) { my $link = $links->{$_}; # do stuff here } } keys %$seen;
      ..and that works like a charm :)

      Thanks again for the reply - its always good to know the reason behind these things, just in case I come across the same thing again in the future :)

      Cheers

      Andy