>>>>> "HK" == H Kern <g...@pbwe.com> 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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to