function call and fetchrow->array or fetchall->arrayref (newbie)

Bobby Kleemann rkleeman at neta.com
Mon Nov 27 18:50:53 CST 2000


~sdpm~
There's several things in your code that I'd rewrite.  Look for my
comments below.

 _ _ _
 Bobby Kleemann <rkleeman at neta.com>
     http://www.neta.com/~rkleeman/

On Mon, 27 Nov 2000, Vasquez, Mike wrote:

> ~sdpm~
> Not sure to which user group  to ask this question.
> 
> Here's the scenerio:
> 
> I have 31 machines, each machine has a name ("machine 01", "machine 02",
> etc.), each machine has an ip address.
> All of this is in an oracle db.
> 
> I've connected to the db.
>   
> I call:  sub{my (@num) = (0, 1, 5); &whatever(@num)}->(),      #####This sub
> will be called 6 times with different values
> 
> sub whatever{
>    $a = @_[0];
>    $b = @_[1];
>    $c = @_[2];

Use either 
my $a = shift; ...

or
my $a = $_[0]; ...

or even
my ($a, $b, $c) = @_;

but *not*
$a = @_[0];  

In this case I think your syntax would put 1 in $a because that's the
number of elements in the array you referenced.  (@_ has three elements,
but you asked for the slice with only one element, the 0th element, in
it.)

>    $location = dbh->quote("machine" . " " . $a . '%');

Why the seperation?  You don't save anything here by pulling them apart
here except for readablity.

$location = $dbh->quote("machine $a%");

>    ###my sql statement is so --
> 	$sth = $dbh->prepare("
>                   SELECT machine_name, ip_address
>                   FROM   machines
>                   where machine_name like $location              
>                   order by machine_name
>            ");
> #### This will return machines 01-09
> 
>    $sth->execute();
>    while(($macinename, $ip) = $sth->fetchrow_array){
>      $testname = $q->param('loc') . ' ' . $a . $b;

Here you may gain a little speed, but is the expense of readability worth
it?

$testname = $q->param('loc') . " $a$b";

> ###$q->pram('loc') value is "machine"
>         if( $testname eq $encname && $b lt $c){

You are using $b and $c as numbers, so you probably want to use the
numeric comparison, < instead of lt.  To make this even clearer, drop the
second half of this test and use the statement proveded below.

>            print $machinename . ' ' $ip . '<br>';

See the above comments on readability.  If you were looking for that
little bit of speed you'd want commas instead of periods.

>        }
>    $b++;

How about "last if (++$b > $c);" so you don't go through some unneccessary
itterations.

>    }
> }
> 
> This prints out ...
>   machine 01
>   machine 02
>   machine 03
>   machine 04
> 
> Problem that I am having is when I try to call this function again like
> this:
>    sub{my (@num) = (0, 5, 9); &whatever(@num)}->(),

Why not like this? "sub { whatever(0, 5, 9) }->();"

> 
> I get the first call returned and this function call returns nothing.
> 
> I'm getting the results from the first function call with the same values.
> I want to be able to call the first function with specific values and then
> call it again with another set of values So the print out result would be
> like this:
>   machine 01
>   machine 02
>   machine 03
>   machine 04
> 
>   machine 05
>   machine 06
>   machine 07
>   machine 08
> 
> Am I using $sth->fetchrow_array correctly or should I be using
> fetchall->arrayref instead?

It looks like you are doing the right thing in the fetchrow_array vs
fetchall->arrayref question.  Try the some of the changes mentioned above
and see if that fixes your problem.

> 
> Any help would be appreciated.
> 
> Mike Vasquez

~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list