MySQL Forums
Forum List  »  Perl

Re: perl help on nested queries
Posted by: Bill Karwin
Date: August 04, 2006 05:33PM

$match{lat} and $match->{lat} are different data structures in Perl.
$match{lat} is an element in the hash array %match, which you have not declared.
$match->{lat} is an element in an anonymous hash array, to which $match is a reference.

The different is subtle, but important, as you have seen. This isn't the sort of thing that is easily apparent to folks who are new to programming in Perl.

In Perl, you can have a scalar $match, an array @match, and a hash array %match, all at the same time. They are three different objects, and they exist independently from one another, they don't have to have the same value, etc.

When you use an element in an array or a hash array, you use the same '$' symbol that you would use for a scalar. The index specifier with [] or {} tells Perl that you mean the array or the hash array, not the simple scalar of the same name.

$match; (this is a scalar)
$match[3]; (this is an array element)
$match{lat}; (this is a hash array element)

Now for the _really_ confusing part: when these $match variables are references, you can get the value of the thing to which it is a reference, depending on the type of thing $match references.

${$match_scalar}; (this is the scalar value to which $match_scalar refers)
${$match_array}[3]; (this is an element of the array to which $match_array refers)
${$match_hash}{lat} (this is an element of the hash array to which $match_hash refers)

I gave them different names above, because a reference is a scalar. Despite the fact that they references are pointing at different types of things, the reference object itself is always a scalar and therefore only one variable by that name can exist.

There are alternative syntax forms for the references:

$$match_scalar;
$match_array->[3]
$match_hash->{lat}

So you can see that $match{lat} and $match->{lat} are quite different objects, and they can even coexist at the same time, despite the similarity in their names.

If your brain hasn't totally melted down yet, read more about Perl references here:
http://search.cpan.org/dist/perl/pod/perlref.pod
http://vergil.chemistry.gatech.edu/resources/programming/perl-tutorial/records.html

Options: ReplyQuote


Subject
Written By
Posted
Re: perl help on nested queries
August 04, 2006 05:33PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.