develooper Front page | perl.beginners | Postings from October 2009

Re: Need explanation about this code.

Thread Previous | Thread Next
From:
Raheel Hassan
Date:
October 9, 2009 00:58
Subject:
Re: Need explanation about this code.
Message ID:
fcb728f20910090058u63e70b4l8bfbfd872f23c4bb@mail.gmail.com
Thanks Jim for a nice reply, could you explain what
push(@$temp_table,$ref->[$i]); is doing. in the first code.

In the second @_= $dbh where as in the if command $_ is getting the values
from this default array. Then how it can return \%s..........in the last
statement.



On Thu, Oct 8, 2009 at 6:54 PM, Jim Gibson <jimsgibson@gmail.com> wrote:

> On 10/8/09 Thu  Oct 8, 2009  9:00 AM, "Raheel Hassan"
> <raheel.hassan@gmail.com> scribbled:
>
> > Hello,
> >
> > I have problems in understanding $#{@$ use ?????
> >
> > 1- my $ref = $$temp_sth -> fetchall_arrayref({});
> >     for(my $i=0; $i <= $#{@$ref}; $i++) {
>
> For any array @a, the largest index is given by $#a.
>
> $ref is a reference to an array, as returned by the fetchall_arrayref
> method.
>
> @{$ref} is the array returned.
>
> Therefore, $#{@$ref} is the largest index of the returned array, i.e., the
> index of the last element of the array.
>
>
> >     push(@$temp_table,$ref->[$i]);}
> >
> > Can some one explain the under given function.
>
> It looks like a function that queries a database, getting all of the data
> from a table called "Sensors". Then for each row in that table, checks the
> value of the column labeled "StatusID", and, if the value of that column is
> "1", adds an entry to a hash with the contents of the "IP" column as key,
> and the value taken from the column "SensorID". The function returns a
> list,
> consisting of a reference to the hash and the value of the $error variable,
> which will be 0 if no error occurred, and 1 if an error did occur.
>
> > 2- sub buildSensorsHashtable
> >     {
> >
> >    my ($dbh) = @_;
> >    my $error = 0;
> >
> >          my $query = "SELECT * FROM Sensors";
> >          my $sth = $dbh->prepare($query);
> >          if(!$sth || !($sth->execute())) { $error = 1 ;
> >  }
> >
> >          my $ref = $sth->fetchall_arrayref({});
> >          my %s;
> >          my @s = @$ref;
> >     foreach (@s)
> >     {
> >         if($_->{"StatusID"} eq "1") { $s{$_->{"IP"}} = $_->{"SensorID"};
> }
> >     }
> >
> >     return (\%s,$error);
> >
> > 3- Which is the best tool for debuging perl programs having CGI support.
> I
> > used DDD but that does not support CGI.
>
> Debugging CGI programs can be difficult. You need access to the HTTP logs.
> You can help yourself by following the advice given in the FAQ "How can I
> get better error messages from a CGI program?" (see 'perldoc -g CGI'). Try
> to set up a test environment, either by executing the CGI program from the
> command line (by supplying the various parameters needed) or by running the
> program first on a local server where you have access to the logs.
>
> Good luck.
>
>
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About