list context

  • Thread starter Gareth Mottram - RSG
  • Start date
G

Gareth Mottram - RSG

Hi all I am having a bit of a problem in putting an array into a hash
structure

<snip>
for($n=0;$n <= 10;$n++)
{
$l[$n]=$n;
}
%current=();
$current{cloud}=(@l);
@x=(@l);
print " @x\n";
print $current{cloud};
</snip

@x happily contains the results of the loop whereas $current{cloud}
stubornly refuses to contain anything but the scalar of the list.
I am now very confused and my brain has melted and leaked all over the
table. Can anyone explain where I am making<pun> a hash of this?</pun>

cheers

g


--
Gareth N. Mottram
Support Officer
Remote Sensing Data Analysis Service
Plymouth Marine Laboratory
Prospect Place
Plymouth
Devon, PL1 3DH
UK

Tel : ++44 (0)1752 633485
Fax : ++44 (0)1752 633101
E-mail: (e-mail address removed)
Web : http://www.npm.ac.uk/rsdas/

Registered Charity No. 1091222
Company No. 4178503
 
P

Paul Lalli

Hi all I am having a bit of a problem in putting an array into a hash
structure

<snip>
for($n=0;$n <= 10;$n++)
{
$l[$n]=$n;
}
%current=();
$current{cloud}=(@l);
@x=(@l);
print " @x\n";
print $current{cloud};
</snip

@x happily contains the results of the loop whereas $current{cloud}
stubornly refuses to contain anything but the scalar of the list.
I am now very confused and my brain has melted and leaked all over the
table. Can anyone explain where I am making<pun> a hash of this?</pun>

You can't put an array inside of a hash. Hash keys and hash values are
each scalars. What you want to do is put a reference to that array into
your hash:

$current{cloud} = \@l;

You would then dereference the entire thing with:

print @{$current{cloud}}, "\n";


for more info: perldoc perldsc
(the data structures cookbook)

Paul Lalli
 
G

gnari

Paul Lalli said:
On Fri, 23 Jan 2004, Gareth Mottram - RSG wrote:

$current{cloud} = \@l;

You would then dereference the entire thing with:

print @{$current{cloud}}, "\n";
and $current{cloud}[2] to access one element
for more info: perldoc perldsc

and
perldoc perllol
perldoc perlreftut
perldoc perlref

gnari
 
T

Tad McClellan

Gareth Mottram - RSG said:
Hi all I am having a bit of a problem in putting an array into a hash
structure

<snip>
for($n=0;$n <= 10;$n++)


You should always have "use strict" enabled when developing Perl code.

You are writing C in Perl. A more Perlish way is:

for my $n ( 0 .. 10 ) {

{
$l[$n]=$n;
}
%current=();
$current{cloud}=(@l);


Scalar on the LHS, so the RHS is in scalar context.

Maybe this is what you wanted to do?

%current = @l;

Can anyone explain where I am making<pun> a hash of this?</pun>


You wanted list context. You got scalar context.
 
D

David K. Wall

Tad McClellan said:
Gareth Mottram - RSG said:
Hi all I am having a bit of a problem in putting an array into a hash
structure
[snip]
%current=();
$current{cloud}=(@l);


Scalar on the LHS, so the RHS is in scalar context.

Maybe this is what you wanted to do?

%current = @l;

Who knows? But that term on the RHS is weird. It would be even weirder if
the OP had written something like

@k = (1..4);
@l = (0..10);
$current{cloud} = (@k, @l);

which would put 11 in $current{cloud}, not 15 or 2. Context, commas, and
terms, oh my!
 
G

Gareth Mottram - RSG

Cheers everyone for your help, that makes a fair bit of sense

g
Hi all I am having a bit of a problem in putting an array into a hash
structure

<snip>
for($n=0;$n <= 10;$n++)
{
$l[$n]=$n;
}
%current=();
$current{cloud}=(@l);
@x=(@l);
print " @x\n";
print $current{cloud};
</snip

@x happily contains the results of the loop whereas $current{cloud}
stubornly refuses to contain anything but the scalar of the list.
I am now very confused and my brain has melted and leaked all over the
table. Can anyone explain where I am making<pun> a hash of this?</pun>

cheers

g

--
Gareth N. Mottram
Support Officer
Remote Sensing Data Analysis Service
Plymouth Marine Laboratory
Prospect Place
Plymouth
Devon, PL1 3DH
UK

Tel : ++44 (0)1752 633485
Fax : ++44 (0)1752 633101
E-mail: (e-mail address removed)
Web : http://www.npm.ac.uk/rsdas/

Registered Charity No. 1091222
Company No. 4178503
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top