How do I reference an array stoerd in a hash?

L

laredotornado

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
 
S

sln

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
 
S

sharma__r

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
 
L

laredotornado

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
 
J

Jürgen Exner

laredotornado said:
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
 
U

Uri Guttman

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
 
J

John W. Krahn

Jürgen Exner said:
No, you don't.


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
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top