Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > list context

Reply
Thread Tools

list context

 
 
Gareth Mottram - RSG
Guest
Posts: n/a
 
      01-23-2004
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
Web : http://www.npm.ac.uk/rsdas/

Registered Charity No. 1091222
Company No. 4178503

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-23-2004
On Fri, 23 Jan 2004, Gareth Mottram - RSG wrote:

> 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
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-23-2004
"Paul Lalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> 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



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-23-2004
Gareth Mottram - RSG <(E-Mail Removed)> wrote:
> 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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      01-23-2004
Tad McClellan <(E-Mail Removed)> wrote:

> Gareth Mottram - RSG <(E-Mail Removed)> wrote:
>> 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!

--
David Wall
 
Reply With Quote
 
Gareth Mottram - RSG
Guest
Posts: n/a
 
      01-26-2004
Cheers everyone for your help, that makes a fair bit of sense

g

Gareth Mottram - RSG wrote:
> 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 Removed)
Web : http://www.npm.ac.uk/rsdas/

Registered Charity No. 1091222
Company No. 4178503
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
return HttpStatusCode.ServiceUnavailable in the context.context.Response.StatusCode (Http response code 503) Flip Rayner ASP .Net 1 01-23-2007 06:35 AM
How to retrieve a session Bean in the Context in a class with the Context reference asd Java 1 11-09-2006 05:00 PM
List context versus list context Bo Lindbergh Perl Misc 12 06-28-2006 07:12 PM
Strange Context Error: Context 0x197ee0 is disconnected in VS 2005 =?Utf-8?B?U3VuU21pbGU=?= ASP .Net 0 01-10-2006 03:59 PM
Context.Items vs Context.Handler (passing values between pages) VS_NET_DEV ASP .Net 2 05-25-2004 01:16 PM



Advertisments