For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > hashes and arrarys









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author hashes and arrarys
The Ghost

2005-06-03, 8:55 pm

First here's my code:

while (my $ref = $dblist -> fetchrow_arrayref ()) {
$surveyQuestions{question}[$counter]= $ref->[0];
$surveyQuestions{displayType}[$counter]=
$ref->
[1];
$surveyQuestions{saveValue}[$counter]=$r
ef->[2];
#Get Answers:
$ref->[2]=~ s/.*\.//;
$baseColumn=$ref->[2];
@answers=$tbl_info->members ($baseColumn);
$surveyQuestions{answers}[$counter]=@ans
wers;
$counter++;
print @answers; # <--- Prints the answers
correctly
}

....

print " Answers:$surveyQuestions{answers}[$x]\n;
# <--- Prints the
number of answers instead

My problem is that I always get the number of answers instead of the
answers. What am I doing wrong?

Xavier Noria

2005-06-03, 8:55 pm

On Jun 3, 2005, at 23:56, The Ghost wrote:

> $surveyQuestions{answers}[$counter]=@ans
wers;


> print " Answers:$surveyQuestions{answers}[$x]\n;
# <--- Prints the
> number of answers instead
>
> My problem is that I always get the number of answers instead of the
> answers. What am I doing wrong?


Perl structures can store just scalars. You need to pass an arrayref
instead of an array, for instance this way:

$surveyQuestions{answers}[$counter] = [ @answers ];

In your code @answers is being evaluated in scalar context, hence
giving the number of answers.

See perldsc.

-- fxn

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com