Returns / list context

C

Chris Newton

Hi all,

I think I've been looking at this too long, now, and I'm just missing
the blindingly obvious...

I want to pass back a sequence of values from a function, to put into a
hash mapping from strings onto string arrays. I tried this (obviously
simplified here) approach:

my %hash;
my $key = "key";

@hash{$key} = values();

sub values
{
return ("value1", "value2");
}

After this, @hash{"key"} seems to be an array containing just the one
string "value1". Why wouldn't this call be working in list context, and
stuffing both values into the hash entry?

Thanks in advance for any insights.

Chris
 
M

Matija Papec

X-Ftn-To: Chris Newton

Chris Newton said:
I think I've been looking at this too long, now, and I'm just missing
the blindingly obvious...

I want to pass back a sequence of values from a function, to put into a
hash mapping from strings onto string arrays. I tried this (obviously
simplified here) approach:

my %hash;
my $key = "key";

@hash{$key} = values();

sub values
{
return ("value1", "value2");
}

First, you'll be probably better with some other function name; "values" is
perl builtin function for returning hash values.

If you want to store an array in hash{$key}, you'll need reference (check
perldoc perlref),

$hash{$key} = [ values() ];

use Data::Dumper; print Dumper \%hash;
After this, @hash{"key"} seems to be an array containing just the one
string "value1". Why wouldn't this call be working in list context, and
stuffing both values into the hash entry?

@hash{"key"} is called hash slice and it is used for reading/populating
multiple hash values at once,

@hash{'one', 'two'} = 1..2;
is pretty same as
($hash{'one'}, $hash{'two'}) = 1..2;
 
G

Gunnar Hjalmarsson

Chris said:
I want to pass back a sequence of values from a function, to put
into a hash mapping from strings onto string arrays. I tried this
(obviously simplified here) approach:

Does your approach include "use warnings;"? It should.
my %hash;
my $key = "key";

@hash{$key} = values();

That line causes a compilation error, since values() is a built-in
Perl function. I assume that you actually are using some other name;
I'm using mysub() below.

It should be noted that @hash{$key} results in a warning, since it is
a slice with only one key. If you had had warnings enabled, that might
have given you enough of a hint to realize what's wrong.
sub values
{
return ("value1", "value2");
}

After this, @hash{"key"} seems to be an array containing just the
one string "value1". Why wouldn't this call be working in list
context, and stuffing both values into the hash entry?

Even if you are calling the function in list context, since the slice
only contains one key, the hash cannot assume more than one value.

You may want to either store an array reference:

$hash{ $key } = [ mysub() ];

or add a key to the hash slice:

my $key = "key";
my $key2 = "key2";

@hash{ $key, $key2 } = mysub();

HTH
 
S

Sherm Pendley

Chris said:
I want to pass back a sequence of values from a function, to put into a
hash mapping from strings onto string arrays. I tried this (obviously
simplified here) approach:

my %hash;
my $key = "key";

Strict, warnings, useless double-quotes, etc.
@hash{$key} = values();

sub values
{
return ("value1", "value2");
}

After this, @hash{"key"} seems to be an array containing just the one
string "value1". Why wouldn't this call be working in list context, and
stuffing both values into the hash entry?

It's not clear what you were expecting. Did you want to assign the array
elements to separate hash elements? If so, you need to use multiple hash
keys in the assignment, like this:

@hash{'key1','key2'} = getvalues();

If you want to associate an array with a single key, you need to write your
sub so that it returns a reference, and then store the reference:

$hash{'key'} = getvalues();
sub getvalues {
return ['value1','value2'];
}

Note that in both cases, I named the sub "getvalues" instead of "values" -
the latter is the name of a built-in sub in Perl.

For more on complex data structures - a *lot* more - see:

perldoc perldsc
perldoc perllol

sherm--
 
C

Chris Newton

Matija Papec wrote...
First, you'll be probably better with some other function name;
"values" is perl builtin function for returning hash values.

Sorry, that was just brain-fade while typing in the simplified version.
The real code uses a different name.
If you want to store an array in hash{$key}, you'll need reference
(check perldoc perlref),

$hash{$key} = [ values() ];

Thanks; I'd forgotten it needed to become a reference when stored in a
hash. Told you it would be obvious! :)

Thanks to all for the replies,
Chris
(who's been programming in too many different languages this week!)
 
M

Matija Papec

X-Ftn-To: Chris Newton

Chris Newton said:
"values" is perl builtin function for returning hash values.

Sorry, that was just brain-fade while typing in the simplified version.
The real code uses a different name.
:)
If you want to store an array in hash{$key}, you'll need reference
(check perldoc perlref),

$hash{$key} = [ values() ];

Thanks; I'd forgotten it needed to become a reference when stored in a
hash. Told you it would be obvious! :)

Thanks to all for the replies,
Chris
(who's been programming in too many different languages this week!)

Is it Python that made you forget all sane things? ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top