> -----Original Message-----
> From: Jerry Preston [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 21, 2001 8:11 AM
> To: begginners
> Subject: lost in a maze of hash
> 
> 
> Hi!
> 
> OK, I am trying to build an return a hash:
> 
>   $list = " 1 2 3 4 5 6 7 8 9 10";
> 
>   $DAT{ 'heading' } = $list;
> 
> I can print it at this time:
> 
> print "$DAT{ 'heading' }*<BR>";
> 
>  or is it better to do it this way?
> 
>  ( @wafer_list ) = split( /\s+/,  $wafer_list );
>   $DAT{ 'heading' } = @wafer_list;

This isn't doing what you want. This is a scalar assignment
(hash entries are always scalars), so @wafer_list is evaluated
in scalar context. An array or list in scalar context evaluates
as the number of elements in the array. so $DAT{heading} is
being set to a number.

To save the array in the hash, you have to store a reference 
to that array in the hash entry. Typically this is done by using
the [ ] anonymous list constructor:

   $DAT{heading} = [ @wafer_list ];

>  
> How can I print it?
> 
> My next issue is that I want to return this data:
> 
>   return \%Dat;
> 
> How do I print the returned data as a string and an array?
> 
> print "$DAT->{ 'heading' }<BR>";

Assuming $DAT is a reference to the %DAT hash, then
$DAT->{heading} will contain an array reference (if you
used my code above). Printing this will simply stringify
the array ref and print something like

   ARRAY(0x400160c4)

To print the contents of the array pointed to by this
reference, you need to dereference the arrayref:

   print "@{$DAT->{heading}}<BR>";

This will print each element, separated by the value in
$" (default space, see perldoc perlvar).
 
> 
> prints only 1;
> 
>   foreach my $heading ( $DAT{ 'heading' } ) {
>     print "  <th>$heading</th>\n";
>   }
> 
> Prints nothing!

You need to always 'use strict' in your programs. I'm
assuming you meant @{$DAT->{heading}} as in the previous
example, but I can't really tell. This is using a
hash %DAT, which may or may not exist in this context.

'use strict' will catch errors like this.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to