Taking a reference to a calculated value

C

Chris Allen

I'm really stuck on the syntax for this one.

I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

But I can't find a single statement that will
do it. I've tried:

$b= \(keys %a);
@$b = keys %a;
$b=\@{keys %a};

They don't do the above - and actually I don't understand
why they do what they do...

Can anybody help?
 
U

Uri Guttman

CA> I'm really stuck on the syntax for this one.
CA> I want to do the equivalent of:

CA> %a=('a'=>1,'b'=>2,'c'=>3);
CA> @temp=keys %a;
CA> $b=\@temp;

CA> But I can't find a single statement that will
CA> do it. I've tried:

CA> $b= \(keys %a);
CA> @$b = keys %a;
CA> $b=\@{keys %a};

CA> They don't do the above - and actually I don't understand
CA> why they do what they do...

anonymous arrays are your friend:

$b = [ keys %a ] ;

uri
 
D

David K. Wall

I'm really stuck on the syntax for this one.

I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

$b = [ keys %a ];

will do it.

See perlref for detailed information, and perlreftut for a tutorial
intro to references.

I realize that the above is just sample code, but just in case, here
is a reference about why enabling strictures ('use strict;') is a
Good Thing: http://perlmonks.org/index.pl?node_id=111088

Also, $a and $b are used as the default input variables to Perl's
sort function, so it's best to avoid them. (Besides that, $a and $b
aren't very descriptive names.)
 
P

Paul Lalli

Chris Allen said:
I'm really stuck on the syntax for this one.

I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

$b = [ keys %a ];

The above creates an anonymous array reference, and populates the
referenced array with the list of values returned by the keys function
But I can't find a single statement that will
do it. I've tried:

$b= \(keys %a);

Here you're generating a list of values, in a (seemingly) random order.
On my machine, that list came out to ('c', 'a', 'b'). The \ operator is
actually being applied to all three elements, effectively creating this
statement:

$b = (\'c', \'a', \'b');

The comma operator used in scalar context returns the last element
evaluated, so you're left with $b being set equal to a reference to the
string 'b'.
@$b = keys %a;

This should work, but is a bit ugly. You're telling Perl $b is an array
reference, and then assigning the list of values returned by keys to the
array referenced by $b. How did this "not work" for you?
$b=\@{keys %a};

This is trying to evaluate whatever is returned by the keys function as
an array reference, dereference it, and then take reference to the
result. I'm not surprised that didn't work. I am surprised it didn't
cause a fatal error, however. I suspect Perl is trying to do something
similar to the first case above, but I'm not positive.
They don't do the above - and actually I don't understand
why they do what they do...

Hope this helps,
Paul Lalli
 
J

John W. Krahn

Chris said:
I'm really stuck on the syntax for this one.

I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

But I can't find a single statement that will
do it. I've tried:

$b= \(keys %a);
@$b = keys %a;
$b=\@{keys %a};

They don't do the above - and actually I don't understand
why they do what they do...

Can anybody help?

I am assuming from the subject line that you want this:

$b = \scalar @temp;


John
 
C

Chris Allen

Chris Allen said:
I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

$b = [ keys %a ];

Thanks to you and everyone else who pointed this out.
This should work, but is a bit ugly. You're telling Perl $b is an array
reference, and then assigning the list of values returned by keys to the
array referenced by $b. How did this "not work" for you?

Ah - my fault entirely on this one - I had been trying things in the
debugger, and had done:

$b=\keys %a; (one of many failed attempts)

@$b = keys %a;

which printed "Not an ARRAY reference".

That'll teach me to be more careful!
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top