Thanks much for your detail explanation.

What if I'm doing a fetch (array refs) within a while
loop and want to assign them to a maxtrix each loop?

before (in my another large clunky program i have
written) I simply fetch and print in the same loop.
Now that I am incorporating your code help for shaping
up the columns I am trying to build the matrix first.

# sth is pointer to a database connection using DBI/DBD
# $ary simply becomes and array reference each loop.
my $row=0;
my @mymatrix;

while ($ary = $sth->fetchrow_arrayref) {
 $mymatrix[$row] = $ary;
 row++;
}

foreach my $line (@mymatrix) {
 print @$line;
}

the above assignment seems to bugger up a bit since the
output is not at all correct. Is this an correct
assignment?








-----Original Message-----
From: Bruce Hudson [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 05, 2004 3:16 PM
To: [EMAIL PROTECTED]
Subject: Re: [Perl-unix-users] need help with matrix's


 
> where I really get confused in Perl is different situations will return
> ARRAY reference and others will return the element.

    I am sure there are clear rules for when perl evalutes in an array
context and when it evaluates in a scalar context but, other than the
obvious ones, for me it is largely trial and error. In a scalar context
the array evaluates as the number of elements in the array rather than
its contents. 

    Evaluating the array versus elements in the array is easier. If you
use the indexing operator (the square brackets) you get elements. If the
result is just one element, use a dollar sign. If more, use an "@". 

> as an example (printf is obviously better), if i were to do a 
>  print @$line . "\n"
> it will return a number so I dont know why that is so. 

    This returns the number because the "." concatenation operator takes
scalar arguments hence the array is evaluated in a scalar context. To get
what you want, use just

                print @$line, "\n";

> also the @$line is confusing to me since I thought @ implied a whole array
> thus meaning  an array reference so I cant seem to explain how @$ works. 

    The "@" says you are dealing with an array, the question is which one.
This syntax refers to "the array refered to by the scalar $line". This is
completely distinct from the variable you would get if you used @line. The
two perl entities $line and @line are completely distinct from each other
despite having the same name. The fact that "$line[0]" is related to @line
rather than $line can be confusing until you get used to it.

    References were added only in later versions of perl so the syntax is
not as clear as it could be. I understand that perl6 is changing the way
this works to clean this up somewhat.

   There, you get:

        @array          == normally returns the array

        scalar @array   == forces scalar context, hence the number of
                                elements in the array

        (@array + 0)    == forces scalar context, hence the number of
                                elements in the array

        $array[0]       == the first element in the array

        @array[0,1,2]   == an array of the first three elements in the
                                array
        @$array         == a completely different array (probably[1])
                                found by following the reference (aka a
                                pointer) found in the scalar $array. It
                                will produce an array if what $array
                                refers to is not an array.

                        [1] You can say "$array = [EMAIL PROTECTED]" in which case
                                it will be the same array but that is
                                unnecessary complexity.
--
Bruce A. Hudson                         | [EMAIL PROTECTED]
UCIS, Networks and Systems              |
Dalhousie University                    |
Halifax, Nova Scotia, Canada            | (902) 494-3405
_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to