in reply to Re: Hash/Associative Array
in thread Hash/Associative Array

Thank you Dave for the encouragement--I sure need some from time to time and this is a good time for it!. I would like to know if you would do something like this?:
my %gGui = {}; $gGui{source} = (my $source,"");
I have done this for one of my variable, but I have hundred of them, and before I change all my code, I would like to know... I'm sure you'll understand!
Thanks again,
Looking forward to hearing from you,
Claire

Replies are listed 'Best First'.
Re^3: Hash/Associative Array
by davido (Cardinal) on Aug 25, 2005 at 07:31 UTC

    Same mistake: my %hGui = {};. Again, that's mistakenly assigning a hashref to an actual hash. It's a juxtaposition of data types. A %hash expects a list. A {} returns a reference, not a list. That's where you're getting the warning you asked about.

    Also, $gGui{source} = ( my $source, "" ) is another problem. $gGui{source} expects a scalar value, not a list. You're trying to provide it with a list. The first element of the list would be undef, because it is the return value from my $source, which since $source isn't asigned a value, is an undefined value. The second element of the list is "" (an empty string). But since $gGui{source} provides scalar context to the expression, there's no list; just the rightmost element in what would have been a list; the empty string, "". Confusing? Yes, but it doesn't have to be.

    The specifics of the comma operator in scalar context are discussed in perlop. But you're missing the bigger picture here somehow.

    Hashes (the datatype) hold a list of scalars, indexed by hash keys. If you assign a list of two elements to a hash, the first element will be taken as a hash key, and the second as a hash value. Ten elements will be five keys and five values. An odd number of elements will throw a warning.

    Hash elements accept scalar values. Scalar values can be a lot of things such as numbers, strings, undef, or references to other scalars, hashes, arrays, or whatever.

    Please do read through perlintro. Follow that by perldata, and then follow up with perlref, perlreftut, and perllol. We're talking about a few hours of reading here, but you'll find yourself greatly enlightened on some concepts that may seem a little complicated at first.


    Dave