On Thu, 2 Oct 2003 17:28:42 -0700 (PDT), <[EMAIL PROTECTED]> wrote:


The inline while loop assignment not working like i wanted. I think the
last fetch assigned or wiped out my var object. Is this is what happening?

Yes. The last time through, fetchrow_array returns an empty array, which you promptly assign to $svc->{dn} and $svc->{email}, making them undefined.


#I WANT this but NOT working
#the last row is ok for me
my $svc = new SVC();
while(($svc->{dn},$svc->{email})=$sth->fetchrow_array)
{ #the $svc var works within the loop
  print $svc->{dn}; #good stuff here
}

#even if successful, it lost the value outside the loop
#I guess it reassign a null to the var on the last fetch try
print $svc->{dn}; #LOST VALUE HERE

--end code--

###this works
while(@row = $sth->fetchrow_array)
{ $svc->{dn}=$row[0];
  $svc->{email}=$row[0];
}

print $svc->{dn}; #good value here

Err.. I think you mean $svc->{email}=$row[1];


But I don't think you can do it without introducing an intermediary variable.

(Variations on a theme - pick whichever you find easiest to read)

# What you've already got
while(@row = $sth->fetchrow_array) {
    $svc->{dn}    = $row[0];
    $svc->{email} = $row[1];
}


# Using list assignment, like what you wanted. while(@row = $sth->fetchrow_array) { ($svc->{dn}, $svc->{email}) = @row; }

# Using a hash slice.
while(@row = $sth->fetchrow_array) {
    @$svc{'dn', 'email'} = @row;
}


Steve


--
Steve Piner
Web Applications Developer
Marketview Limited
http://www.marketview.co.nz

Reply via email to