On Tue, Aug 21, 2001 at 07:10:50AM -0500, Jerry Preston shaped the electrons to read:
> 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;
>  

Firstly 'cos you have a space in the front of the 1 in $list ... you will get
one additional element in @wafer_list ...

Secondly a hash can only store a scalar but you are trying to squeeze an array
in!  What will happen is that 1 (true value I think) is put into your hash as
value.  You should be using a reference to an array ... like so
        $DAT{'heading'} = \@wafer_list;

To get each element u would have to do this ..

foreach my $element (@{$DAT{heading}}) { print $element; }

> How can I print it?
> 
> My next issue is that I want to return this data:
> 
>   return \%Dat;
>

You are returning a reference to a hash .. thus the returned value is a
reference ...

> How do I print the returned data as a string and an array?
>

You would need to dereference the reference to use the value ... like so 
my $ref_to_hash = sub_that_returns_dat_as_ref();
foreach my $element ( @{$ref_to_hash->{heading}} ) { print $element };

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

$DAT->{'heading'} is a reference to an array .. not an array ...

> prints only 1;
> 
>   foreach my $heading ( $DAT{ 'heading' } ) {
>     print "  <th>$heading</th>\n";
>   }
>

$DAT{'heading'} is a reference to an array (so it is actually a scalar) ...

You seem to have arrays,hashes and references all mixed up ... I would suggest
using Data::Dumper in your scripts so that you can see the data structure and
have a clearer picture of what your data actually looks like ...

you need to add use Data::Dumper to somewhere near the top of your script.
You can then use the function Dumper (which takes a reference to naything as
an argument and return the data structure of the reference ...

for eg

use strict;
use Data::Dumper;

my $string = "1 2 3 4 5";
my @array = split(/\s+/,$string);
my %hash = ( 'a' => 1 , 'b' => 2 );
print Dumper(\@array),"\n";
print Dumper(\%hash),"\n";

Let me know if this is confusing ..

> Prints nothing!
> 
> Thanks for your help,
> 
> Jerry
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

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

Reply via email to