list context

Discussion in 'Perl Misc' started by Gareth Mottram - RSG, Jan 23, 2004.

  1. 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:
    Web : http://www.npm.ac.uk/rsdas/

    Registered Charity No. 1091222
    Company No. 4178503
    Gareth Mottram - RSG, Jan 23, 2004
    #1
    1. Advertising

  2. Gareth Mottram - RSG

    Paul Lalli Guest

    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
    Paul Lalli, Jan 23, 2004
    #2
    1. Advertising

  3. Gareth Mottram - RSG

    gnari Guest

    "Paul Lalli" <> wrote in message
    news:...
    > 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
    gnari, Jan 23, 2004
    #3
  4. 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++)



    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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 23, 2004
    #4
  5. Tad McClellan <> wrote:

    > Gareth Mottram - RSG <> 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
    David K. Wall, Jan 23, 2004
    #5
  6. 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:
    Web : http://www.npm.ac.uk/rsdas/

    Registered Charity No. 1091222
    Company No. 4178503
    Gareth Mottram - RSG, Jan 26, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,790
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    691
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    639
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    423
    www.pulpjava.com
    Nov 9, 2006
  5. Bo Lindbergh

    List context versus list context

    Bo Lindbergh, Jun 21, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    215
    Charles DeRykus
    Jun 28, 2006
Loading...

Share This Page