Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI

by roboticus (Chancellor)
on Jun 15, 2007 at 12:51 UTC ( [id://621456]=note: print w/replies, xml ) Need Help??


in reply to Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI

cool:

No offense intended, but you should trim down your code before sending a question like that. Many will skip the node if they have to work too hard to see the problem. (See nodes like How (Not) To Ask A Question).

Anyway, on to your question. Based on your statement and the code, I'd imagine that your problem is that you process the data this way:

while (($endstore[$ctrend][0],$endstore[$ctrend][1],$endstore[$ctren +d][2],$endstore[$ctrend][3],$endstore[$ctrend][4])= my @arr)= $sth2-> +fetchrow_array() ) { my ($field1, $field2, $field3,$field4,$field5)=@arr; $endflag=2; print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $field3 F +ield 4:$field4 Field5: $field5\n"; ++$ctrend; }
The $sth2->fetchrow_array() call returns false when it has nothing to report back, and you're assigning that to an array. Move the extra assignment into your loop so it's only copied if you have a row of data:

while (my @arr = $sth2->fetchrow_array() ) { # Keep a copy of the data for future use ($endstore[$ctrend][0],$endstore[$ctrend][1],$endstore[$ctrend][2] +,$endstore[$ctrend][3],$endstore[$ctrend][4])= @arr; my ($field1, $field2, $field3,$field4,$field5)=@arr; $endflag=2; print STDOUT "Field 1: $field1 Field 2: $field2 Field 3: $field3 + Field 4:$field4 Field5:$field5\n"; ++$ctrend; }
But you really need to start working on structuring your programs to make them simpler to read. This will help make your code more maintainable, as well as making it easier to steal bits from for other projects.

Use indentation liberally, don't be afraid of blank lines, and use meaningful variable names. I'm not going to take on your entire script, but let's work on this loop for a moment. A little bit of rearranging will make it a little easier to read:

while (my @arr = $sth2->fetchrow_array() ) { # Keep a copy of the data for future use ($endstore[$ctrend][0], $endstore[$ctrend][1], $endstore[$ctrend][2], $endstore[$ctrend][3], $endstore[$ctrend][4]) = @arr; my ($field1, $field2, $field3, $field4, $field5) = @arr; $endflag=2; print STDOUT "Field 1: $field1 Field 2: $field2 " . "Field 3: $field3 Field 4: $field4 " . "Field 5: $field5\n"; ++$ctrend; }
Now I may have overformatted it, but it certainly makes it easier to see what you're doing.

--roboticus

  • Comment on Re: Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI
by ikegami (Patriarch) on Jun 15, 2007 at 17:49 UTC
    ($endstore[$ctrend][0], $endstore[$ctrend][1], $endstore[$ctrend][2], $endstore[$ctrend][3], $endstore[$ctrend][4]) = @arr;

    can be abbrieviated to

    @{$endstore[$ctrend]}[0..3] = @arr[0..3];

    If I read the intent correctly, it can be abbrieviated further to

    $endstore[$ctrend] = [ @arr ];

    Of course, since $ctrend is always being incremented, we simply want

    push @endstore, [ @arr ];

    Final result:

    while (my @arr = $sth2->fetchrow_array() ) { push @endstore, [ @arr ]; my ($field1, $field2, $field3, $field4, $field5) = @arr; $endflag=2; print STDOUT "Field 1: $field1 Field 2: $field2 " . "Field 3: $field3 Field 4: $field4 " . "Field 5: $field5\n"; }

    Note that [ @arr ] can't be replaced by \@arr since you'd be pushing multiple references to the same array.

      ikegami:

      I thought about simplifying it further, but I didn't want to "go all perlmonks on him". ;^)

      Actually, I was going to go further and try to simplify the endstore assignment, but I wasn't comfortable enough in my perliness to be certain the best way to do it. (I've been doing C++ for a while lately, and it takes my brain a bit to shift gears...)

      --roboticus

      Update: Added hyperlink to quote...

      Dear ikegami,

      Actually something just opposite I wanted; I wanted monks to go all monks way go all perlmonks on him, but to phrase that in such way was a problem :) To tell you the truth, I have learnt anonymous reference Re^2: create hash names dynamically just 5 days ago and while learing DBI got the opportunity to use that. But after writing/working I knew there must be shorter ways to do that. But it would come to this, I did not have idea. So frankly I am impressed, once again. And to add further to that, would love to be bombarded by samego all perlmonks on him way, all the time.

      Thank you for showering of wisdom.
      Love you all monks :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://621456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-25 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found