second post

second post

am 21.07.2011 06:34:42 von H Kern

Just a followup on that last one...

I'm using this code thinking that ($header{"info}) will be an array of all
the elements in the string that was read in.

The first print statement does what I hope, printing out the elements of
that array as a string.

But, when the perl compiler gets to the second print statement, it balks.

"Can't use string ("HDR QuickBooks Premier Version 2"...) as an
ARRAY ref while "strict refs" in use at..."

This makes me think that in the fourth line '( $header{"info"} ) =
split(/\t\n/, <>);' $header{"info"} is actually getting set to a string
of the concatenation of the parts split apart by the split function. Is
this true?

If so, then how do I set the hash table element header{"info"} to be the
array of elements created by the split() function?

.... and, can the elements of that array be accessed using the syntax I use
in the second print statement?

Thanks, --H

code:

my %header;

( $header{"keys"} ) = split(/\t\n/, <>);
( $header{"info"} ) = split(/\t\n/, <>);

print $header{"info"} . "\n";
print $header{"info"}[0] . "\n";

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: second post

am 21.07.2011 06:49:36 von jbiskofski

If you want to store an array inside a hash value you have to store a refere=
nce to it, not the actual array. Here are two examples :

my %hash;
my @arr =3D split(...);
$hash{'key'} =3D \@arr;

Or shorter :

$hash{'key'} =3D [ split(...) ];

Cheers dude!

-- biskofski

Sent from my iPad

On Jul 20, 2011, at 11:34 PM, "H Kern" wrote:

> Just a followup on that last one...
>=20
> I'm using this code thinking that ($header{"info}) will be an array of all=
the elements in the string that was read in.
>=20
> The first print statement does what I hope, printing out the elements of t=
hat array as a string.
> But, when the perl compiler gets to the second print statement, it balks.
>=20
> "Can't use string ("HDR QuickBooks Premier Version 2"...) as an ARRA=
Y ref while "strict refs" in use at..."
> This makes me think that in the fourth line '( $header{"info"} ) =3D split=
(/\t\n/, <>);' $header{"info"} is actually getting set to a string of the c=
oncatenation of the parts split apart by the split function. Is this true?
>=20
> If so, then how do I set the hash table element header{"info"} to be the a=
rray of elements created by the split() function?
>=20
> ... and, can the elements of that array be accessed using the syntax I use=
in the second print statement?
>=20
> Thanks, --H
>=20
> code:
>=20
> my %header;
>=20
> ( $header{"keys"} ) =3D split(/\t\n/, <>);
> ( $header{"info"} ) =3D split(/\t\n/, <>);
>=20
> print $header{"info"} . "\n";
> print $header{"info"}[0] . "\n";
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: second post

am 21.07.2011 06:57:00 von Uri Guttman

>>>>> "HK" == H Kern writes:

HK> Just a followup on that last one...
HK> I'm using this code thinking that ($header{"info}) will be an array of
HK> all the elements in the string that was read in.

HK> But, when the perl compiler gets to the second print statement, it balks.

HK> "Can't use string ("HDR QuickBooks Premier Version 2"...) as an
HK> ARRAY ref while "strict refs" in use at..."

HK> This makes me think that in the fourth line '( $header{"info"} ) =
HK> split(/\t\n/, <>);' $header{"info"} is actually getting set to a
HK> string of the concatenation of the parts split apart by the split
HK> function. Is this true?

you are assigning a single value from the split, not an array. you can't
assign arrays in perl to scalars. you can only assign array
references. read perldoc perlreftut to get going with references. it is
a core feature you need to learn to get into perl data structures.

HK> ... and, can the elements of that array be accessed using the syntax I
HK> use in the second print statement?

well, no as you just assigned a single string into that element. it is
not an array reference. you dereferenced it and perl says you don't have
a reference in there so die!

HK> ( $header{"keys"} ) = split(/\t\n/, <>);
HK> ( $header{"info"} ) = split(/\t\n/, <>);

this will do what you want. note the () are gone as the [] provides the
list context for split. it splits, makes a list, that is stored in the
anon array ref [] and that is stored in the hash.

$header{"info"} = [ split(/\t\n/, <>) ] ;

HK> print $header{"info"} . "\n";

that would print out the string you assigned. with my change it will
print the array ref itself (not its data)

HK> print $header{"info"}[0] . "\n";

that would dereference the string which dies. with my change it will
print the first element of the list from split.

uri

--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/