Array in a hash question?

Discussion in 'Perl Misc' started by Jamie Smyth, May 29, 2004.

  1. Jamie Smyth

    Jamie Smyth Guest

    I have a hash defined as,

    my %data = (
    'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
    'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
    );

    and I would like to extract the data in the @spc array. I tried,

    my @spc = %data{'spc'};

    and

    my $spc = %data{'spc'};

    but this results in the array being created directly in @spc[0]. I
    ended up using:

    my $d = $data{'spc'};
    my @data = ();
    push @data, @$d;

    which gives me a 1dimensional array @data that I can use... the
    problem is that I don't really know why this works and it looks a
    little weird. Can someone explain?

    Thanks.
    Jamie Smyth, May 29, 2004
    #1
    1. Advertising

  2. Jamie Smyth

    Matt Garrish Guest

    "Jamie Smyth" <> wrote in message
    news:...
    > I have a hash defined as,
    >
    > my %data = (
    > 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
    > 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
    > );
    >
    > and I would like to extract the data in the @spc array. I tried,
    >
    > my @spc = %data{'spc'};
    >


    The value for key spc is a reference to an array (hash values are always
    scalars). In order to access the array, you need to dereference the
    reference:

    my @array = @{ $data{'spc'} };

    Matt
    Matt Garrish, May 30, 2004
    #2
    1. Advertising

  3. Jamie Smyth <> wrote:
    > I have a hash defined as,
    >
    > my %data = (
    > 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
    > 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
    > );
    >
    > and I would like to extract the data in the @spc array.



    Apply "Use Rule 1" from:

    perldoc perlreftut


    1) pretend is is a plain array

    my @spc = @data;

    2) replace the _name_ with a block ...

    my @spc = @{ };

    3) ... that returns a reference to an array

    my @spc = @{ $data{spc} };



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 30, 2004
    #3
  4. Matt Garrish wrote:
    >
    > "Jamie Smyth" <> wrote in message
    > news:...
    > > I have a hash defined as,
    > >
    > > my %data = (
    > > 'wn_l' => $wn_l, 'wn_u' => $wn_u, 'wn_d' => $wn_d,
    > > 'npts' => $#spc+1, 'spc' => [@spc], 'label' => $label,
    > > );
    > >
    > > and I would like to extract the data in the @spc array. I tried,
    > >
    > > my @spc = %data{'spc'};

    >
    > The value for key spc is a reference to an array (hash values are always
    > scalars). In order to access the array, you need to dereference the
    > reference:
    >
    > my @array = @{ $data{'spc'} };


    Also, the 'npts' entry isn't required as the array in scalar context
    will return the number of elements:

    my $npts = @{ $data{ spc } };


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, May 30, 2004
    #4
    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. rp
    Replies:
    1
    Views:
    490
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    258
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    214
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    591
    David A. Black
    Jul 2, 2008
  5. Replies:
    11
    Views:
    177
    Eric Schwartz
    Oct 10, 2005
Loading...

Share This Page