2

So, this doesn't work, and I have no idea why. I've tried every possible variation. But nothing works. I'm ready to take a chainsaw to my server, but hopefully you can prevent that:

sub getQuestMarkers {
  #database stuff
  ...
  my %package;
  while(my ($key, $lat, $lng) = $sth->fetchrow_array()) {
    $package{$key} = ($lat,$lng);
  }

  ...
  return %package;
}

my %markers = getQuestMarkers();
while(my( $key, $value) = each %markers) {
  print "$key: @value - $value[0] $value[1]\n";
}
2
  • 2
    perldoc perlop: "Binary , is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator." Commented Apr 8, 2017 at 9:52
  • 3
    Always start with use strict; use warnings;. That would have told you that @value doesn't exist (it wasn't declared anywhere). Commented Apr 8, 2017 at 9:53

1 Answer 1

3

Use brackets [ ] to create an array reference, not parens ( );

As written, your code throws away the first value $lat. Write it like this instead:

$package{$key} = [$lat,$lng];

You can pull out the values like this:

my ($lat,$lng) = @{ $package{$key} };

In your code, you could print out the values by dereferencing them:

print "$key: " . $value->[0] . " " . $value->[1] . "\n";

Have a look at perldoc perlreftut.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This has been frustrating me for days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.