Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > I am needing a gentle introduction to accessing a perl array from areference

Reply
Thread Tools

I am needing a gentle introduction to accessing a perl array from areference

 
 
Larry W. Virden
Guest
Posts: n/a
 
      01-10-2009
On Jan 10, 4:17 am, "Peter J. Holzer" <(E-Mail Removed)> wrote:
> Since the rest of the code looks like it expects
> an array of hashrefs I have to ask whether you are sure that this is
> really the code which is called. Or is there a very similar method which
> differs only in calling fetchrow_hashref instead of fetchrow_array, and
> you are calling that instead?


Well

$ grep fetchrow_hash user_info.pl
$ getmanlist.pl


 
Reply With Quote
 
 
 
 
Larry W. Virden
Guest
Posts: n/a
 
      01-10-2009
On Jan 10, 8:17 am, "Larry W. Virden" <(E-Mail Removed)> wrote:
> On Jan 10, 4:17 am, "Peter J. Holzer" <(E-Mail Removed)> wrote:
>
> > Since the rest of the code looks like it expects
> > an array of hashrefs I have to ask whether you are sure that this is
> > really the code which is called. Or is there a very similar method which
> > differs only in calling fetchrow_hashref instead of fetchrow_array, and
> > you are calling that instead?

>
> Well
>
> $ grep fetchrow_hash user_info.pl
> $ getmanlist.pl


whoops - that Enter key just sent the posting, instead of moving to
the next line. Sigh.
anyways, when I run getmanlist.pl (the program that treats the value
returned as a hash) I get values back from the line that uses the
hash. Ah - I see the reason for the descrepancy. As I mentioned, the
web service has a lot of methods, and the one I was thinking was the
one being called was not.
I have problems with focusing with my trifocals. Here's the code the
function being called is using:

sub GetManagerList{

my (@next_row);
my @retval; #this will be array of hashes


#query db to find information
my ($dbconn) = DBI->connect("DBI:Oracle:$DBHOST",$DBUSER,$DBPASS) or
return \@retval;
my ($query) = $dbconn->prepare(
"SELECT h.*, c.UNIX_UID from CSI_HR
h, CSI_CORE c WHERE h.ALT_EMP_NO = c.HR_ALT_EMP_NO and EXISTS
(Select * FROM CSI_HR h2 where
h.Alt_Emp_No = h2.Supervisor_Emp_No) order by Last_Name");

$query->execute() or return \@retval; #problem return empty array


@next_row= $query->fetchrow_array();
while (@next_row) #get rows and add to return array of hashes
{

#get UID and Name

push(@retval,{Alt_Emp_No => $next_row[0], Job_Class => $next_row
[1], Title => $next_row[2],Dept_Code => $next_row[3],
Dept_No => $next_row[4],Last_Name => $next_row
[5],First_Name => $next_row[6],Middle_Name => $next_row[7],
Supervisor_Emp_No => $next_row[8], HR_Status =>
$next_row[9],
HR_Create => $next_row[10],
HR_Mod => $next_row[11], User_Name => scalar getpwuid
($next_row[12])});

@next_row= $query->fetchrow_array();
}

$query->finish;
$dbconn->disconnect();
return \@retval;
}

Anow now that I found the right function, I see the code manufactures
its own hash in the return value from the flat arrays. That makes so
much more sense. I kept trying to figure out how the other code was
doing what it was doing, and it was causing me to misunderstand how
perl in general worked. Now things are much more clear.

Thank you all for your help!
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      01-10-2009
Larry W. Virden wrote:

> @next_row= $query->fetchrow_array();
> while (@next_row) #get rows and add to return array of hashes


Yuck, see "{ Slice => {} }" in the DBI doc.

--
Ruud
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-10-2009
Larry W. Virden <(E-Mail Removed)> wrote:
> On Jan 10, 8:17 am, "Larry W. Virden" <(E-Mail Removed)> wrote:
>> On Jan 10, 4:17 am, "Peter J. Holzer" <(E-Mail Removed)> wrote:
>>
>> > Since the rest of the code looks like it expects
>> > an array of hashrefs I have to ask whether you are sure that this is
>> > really the code which is called. Or is there a very similar method which
>> > differs only in calling fetchrow_hashref instead of fetchrow_array, and
>> > you are calling that instead?


> Ah - I see the reason for the descrepancy. As I mentioned, the
> web service has a lot of methods, and the one I was thinking was the
> one being called was not.



> Here's the code the
> function being called is using:



As you may have already deduced, this is poor quality code.

I give it a C+.


> sub GetManagerList{
>
> my (@next_row);
> my @retval; #this will be array of hashes
>
>
> #query db to find information
> my ($dbconn) = DBI->connect("DBI:Oracle:$DBHOST",$DBUSER,$DBPASS) or
> return \@retval;



Programs should not fail silently. They should fail noisily.

When the user gets no results, they cannot know if that is because
there were actually no results or if it was because a connection
to the database could not be established!


> my ($query) = $dbconn->prepare(
> "SELECT h.*, c.UNIX_UID from CSI_HR
> h, CSI_CORE c WHERE h.ALT_EMP_NO = c.HR_ALT_EMP_NO and EXISTS
> (Select * FROM CSI_HR h2 where
> h.Alt_Emp_No = h2.Supervisor_Emp_No) order by Last_Name");



Check the return value for the statement before this one.

Check the return value for the statement after this one.

Do not check the return value for this statement?


Using "select *" is a very bad practice.

If the table was defined like this:

create table test (
address text,
name text
);

then you get address followed by name from "select *".

If the table was defined like this:

create table test (
name text,
address text
);

then you get name followed by address from "select *".


> $query->execute() or return \@retval; #problem return empty array
>
>
> @next_row= $query->fetchrow_array();
> while (@next_row) #get rows and add to return array of hashes
> {
>
> #get UID and Name
>
> push(@retval,{Alt_Emp_No => $next_row[0], Job_Class => $next_row
> [1], Title => $next_row[2],Dept_Code => $next_row[3],
> Dept_No => $next_row[4],Last_Name => $next_row
> [5],First_Name => $next_row[6],Middle_Name => $next_row[7],
> Supervisor_Emp_No => $next_row[8], HR_Status =>
> $next_row[9],
> HR_Create => $next_row[10],
> HR_Mod => $next_row[11], User_Name => scalar getpwuid
> ($next_row[12])});
>
> @next_row= $query->fetchrow_array();
> }



while ( my $href = fetchrow_hashref ) {
push @retval, $href;
}

Does (probably) the same thing in 2 lines instead of in 7 lines...


> Anow now that I found the right function, I see the code manufactures
> its own hash in the return value from the flat arrays. That makes so
> much more sense.



That makes more sense with regard to how you're seeing what you're seeing.

It makes a great deal less sense with regard to how the hash is getting built.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question about python garbage collection when keeping only areference to an object's member George Burdell Python 3 11-12-2010 10:20 PM
Where is the document for \ (backslash operator for creating areference) Peng Yu Perl Misc 2 06-05-2010 05:12 AM
A not so gentle hint. Feliks Dzerzhinsky Windows 64bit 16 04-27-2007 07:30 PM
Be gentle on the newbies Tux Digital Photography 3 07-11-2004 03:19 AM
Open source on Windows - a gentle introduction to freedom Bottom Line Computer Computer Security 10 06-09-2004 07:29 PM



Advertisments