How do I reference an array stoerd in a hash?

Discussion in 'Perl Misc' started by laredotornado, Oct 15, 2009.

  1. Hi,

    I have a hash with a scalar as a key and an array as a value.
    Hopefully, I'm setting it up right, but when I try and print my array,
    the only thing that prints out is the first value. How do I rewrite
    the below to print back all values in the array?

    my %field_fns = ();
    $field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank',
    'MailingZipNull');
    print $field_fns{'MAIL_ZIP'};

    Thanks, - Dave
    laredotornado, Oct 15, 2009
    #1
    1. Advertising

  2. laredotornado

    Guest

    On Thu, 15 Oct 2009 13:33:51 -0700 (PDT), laredotornado <> wrote:

    >Hi,
    >
    >I have a hash with a scalar as a key and an array as a value.
    >Hopefully, I'm setting it up right, but when I try and print my array,
    >the only thing that prints out is the first value. How do I rewrite
    >the below to print back all values in the array?
    >
    >my %field_fns = ();
    >$field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank','MailingZipNull');

    ^ ^
    the paren's make this a list, not an array.

    >print $field_fns{'MAIL_ZIP'};


    usually arrays have to be itterated over, they won't just print all at once.

    >
    >Thanks, - Dave


    $field_fns{'MAIL_ZIP'}
    ^
    $ this denotes a 'scalar' variable not an array.
    The scalar can contain only one value.

    You can however, assign a reference (a scalar value) to an array,
    so $field_fns{'MAIL_ZIP'} = reference to an array.

    There are two kinds of arrays, named and anonomyous.
    @named_array = (); # empty array
    You can take a reference to anything with the '\' backslash.
    So you could have done:

    @named_array = ('MailingZipInvalid', 'MailingZipBlank','MailingZipNull');
    $field_fns{'MAIL_ZIP'} = \@named_array;

    You could also have done anonymous array (denoted by []).

    $array_reference = ['MailingZipInvalid', 'MailingZipBlank','MailingZipNull'];
    $field_fns{'MAIL_ZIP'} = $array_reference;
    # or simply
    $field_fns{'MAIL_ZIP'} = ['MailingZipInvalid', 'MailingZipBlank','MailingZipNull'];

    Its still an array, but the scalar $field_fns{'MAIL_ZIP'} holds a single value, a reference.

    To print it out, itterate over it.

    for my $mail_info (@{$field_fns{'MAIL_ZIP'}))
    {
    print "$mail_info \n";
    }

    or via magic,

    print "@{$field_fns{'MAIL_ZIP'}) \n";

    Anyway, the fancy notation accessing the array is called
    dereferencing a reference.

    You have a lot to read though.

    -sln
    , Oct 15, 2009
    #2
    1. Advertising

  3. laredotornado

    Guest

    On Oct 16, 1:33 am, laredotornado <> wrote:
    > Hi,
    >
    > I have a hash with a scalar as a key and an array as a value.
    > Hopefully, I'm setting it up right, but when I try and print my array,
    > the only thing that prints out is the first value.  How do I rewrite
    > the below to print back all values in the array?
    >
    > my %field_fns = ();
    > $field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank',
    > 'MailingZipNull');
    > print $field_fns{'MAIL_ZIP'};
    >
    > Thanks, - Dave



    The point to remember here is that a hash in perl holds key=>value
    pairs,
    wherein, both key & value are supposed to be SCALARS.

    So when you intend to store an array as value, then you need to
    convert that
    into a scalar somehow. And that is via taking the reference to that
    array
    & using that reference as the value portion in your hash.

    One way to do that is to use the square-bracket notation instead of
    the parens, like, e.g.,

    $field_fns{'MAIL_ZIP'} = [ 'MailingZipInvalid',
    'MailingZipBlank','MailingZipNull', ];

    But now if you were to type this: print $field_fns{'MAIL_ZIP'}; you'd
    get something like ARRAY(0x32435)
    That's because the value is a reference & that's how a reference looks
    from inside.

    To get to the array being referred to , we would need to dereference
    the value by enrobing it in @{ ... }

    my @array = @{ $field_fns{'MAIL_ZIP'} };


    -- Rakesh
    , Oct 15, 2009
    #3
  4. On Oct 15, 2:57 pm, wrote:
    > On Thu, 15 Oct 2009 13:33:51 -0700 (PDT), laredotornado <> wrote:
    > >Hi,

    >
    > >I have a hash with a scalar as a key and an array as a value.
    > >Hopefully, I'm setting it up right, but when I try and print my array,
    > >the only thing that prints out is the first value.  How do I rewrite
    > >the below to print back all values in the array?

    >
    > >my %field_fns = ();
    > >$field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank','MailingZipNull');

    >
    >                           ^                                                      ^
    > the paren's make this a list, not an array.
    >
    > >print $field_fns{'MAIL_ZIP'};

    >
    > usually arrays have to be itterated over, they won't just print all at once.
    >
    >
    >
    > >Thanks, - Dave

    >
    >   $field_fns{'MAIL_ZIP'}
    >   ^
    > $ this denotes a 'scalar' variable not an array.
    > The scalar can contain only one value.
    >
    > You can however, assign a reference (a scalar value) to an array,
    > so   $field_fns{'MAIL_ZIP'} = reference to an array.
    >
    > There are two kinds of arrays, named and anonomyous.
    > @named_array = (); # empty array
    > You can take a reference to anything with the '\' backslash.
    > So you could have done:
    >
    >   @named_array = ('MailingZipInvalid', 'MailingZipBlank','MailingZipNull');
    >   $field_fns{'MAIL_ZIP'} = \@named_array;
    >
    > You could also have done anonymous array (denoted by []).
    >
    >   $array_reference = ['MailingZipInvalid', 'MailingZipBlank','MailingZipNull'];
    >   $field_fns{'MAIL_ZIP'} = $array_reference;
    >   # or simply
    >   $field_fns{'MAIL_ZIP'} = ['MailingZipInvalid', 'MailingZipBlank','MailingZipNull'];
    >
    > Its still an array, but the scalar $field_fns{'MAIL_ZIP'} holds a single value, a reference.
    >
    > To print it out, itterate over it.
    >
    >   for my $mail_info (@{$field_fns{'MAIL_ZIP'}))
    >   {
    >         print "$mail_info \n";
    >   }
    >
    > or via magic,
    >
    >   print "@{$field_fns{'MAIL_ZIP'}) \n";
    >
    > Anyway, the fancy notation accessing the array is called
    > dereferencing a reference.
    >
    > You have a lot to read though.
    >
    > -sln


    Perfect! 5 stars. - Dave
    laredotornado, Oct 15, 2009
    #4
  5. laredotornado <> wrote:
    >I have a hash with a scalar as a key and an array as a value.


    No, you don't.

    >Hopefully, I'm setting it up right, but when I try and print my array,
    >the only thing that prints out is the first value. How do I rewrite
    >the below to print back all values in the array?
    >
    >my %field_fns = ();
    >$field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank',
    >'MailingZipNull');


    You are creating a list "(...)" and then using it in scalar context
    ($..... is always a scalar). And the scalar value of a list is its first
    element, that's why you are getting the first element only.

    Instead you first need to create an array and second assign a reference
    to that array to the hash element. The "[...]" notation will
    conveniently do both by creating an anonymous array:

    $field_fns{'MAIL_ZIP'} = ['MailingZipInvalid',
    'MailingZipBlank',
    'MailingZipNull'];


    >print $field_fns{'MAIL_ZIP'};


    And this will print the reference now. Therefore you need to
    de-reference the refernce to the array by simply following rule #1 from
    'perldoc perlreftut":

    print @{$field_fns{'MAIL_ZIP'}};

    jue
    Jürgen Exner, Oct 16, 2009
    #5
  6. laredotornado <> wrote:
    >On Oct 15, 2:57 pm, wrote:
    >> usually arrays have to be itterated over, they won't just print all at once.


    Nonsense, of course print() will happily print a whole array at once.

    jue
    Jürgen Exner, Oct 16, 2009
    #6
  7. laredotornado

    Uri Guttman Guest

    >>>>> "JE" == Jürgen Exner <> writes:

    JE> You are creating a list "(...)" and then using it in scalar context
    JE> ($..... is always a scalar). And the scalar value of a list is its first
    JE> element, that's why you are getting the first element only.

    this is a classic misunderstanding. there is no such thing as a list in
    a scalar context. there is list context and scalar context. a comma
    separated expression in scalar context is just a series of comma
    ops. and comma ops return their right arg so the last value will be
    returned in a series of them.

    perl -le '$x = qw(a b c); print $x'
    c
    perl -le '$x = ("a", "b", "c"); print $x'
    c

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Oct 16, 2009
    #7
  8. Jürgen Exner wrote:
    > laredotornado <> wrote:
    >> I have a hash with a scalar as a key and an array as a value.

    >
    > No, you don't.
    >
    >> Hopefully, I'm setting it up right, but when I try and print my array,
    >> the only thing that prints out is the first value. How do I rewrite
    >> the below to print back all values in the array?
    >>
    >> my %field_fns = ();
    >> $field_fns{'MAIL_ZIP'} = ('MailingZipInvalid', 'MailingZipBlank',
    >> 'MailingZipNull');

    >
    > You are creating a list "(...)" and then using it in scalar context


    There is no such thing as a list in scalar context.


    > ($..... is always a scalar). And the scalar value of a list is its first
    > element, that's why you are getting the first element only.


    Actually, because of how the comma operator works you get the last
    element of the list.

    $ perl -le'$field_fns{"MAIL_ZIP"} = ("MailingZipInvalid",
    "MailingZipBlank", "MailingZipNull"); print $field_fns{"MAIL_ZIP"}'
    MailingZipNull





    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
    John W. Krahn, Oct 16, 2009
    #8
    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:
    520
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    272
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    231
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    617
    David A. Black
    Jul 2, 2008
  5. Steve

    hash reference as a hash key

    Steve, Sep 26, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    117
    Steve
    Sep 28, 2003
Loading...

Share This Page