Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Capture output of a while loop into a variable

by onelesd (Pilgrim)
on Sep 27, 2011 at 18:30 UTC ( #928138=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Capture output of a while loop into a variable
in thread Capture output of a while loop into a variable

By following the principle of Separation of Concerns you would make your code more clear and easier to maintain. Beyond that, if this code is part of a larger project and you are displaying lots of HTML, then following CountZero's suggestion would be a good idea, but I'm not going to do that in this example.

Just be aware of OOM issues since you would be storing all rows in memory up to 2x - once in CQ and once in perl - but at least 1x in perl depending on how CQ implements cursors.

Also note that following SoC will sometimes make your code less efficient, like this case where we use multiple loops when we could have collapsed them such as in my first reply, but that's a judgement call for you to make on legibility & maintainability vs. efficiency.

Btw, you should use strict; use warnings;. Hope this helps!

$resultSet->Execute() or die "resultset error\n" ; my @all_rows ; push @all_rows, { Environment => $resultSet->GetColumnValue(1), Release => $resultSet->GetColumnValue(2), Project => $resultSet->GetColumnValue(3), VLS_Name => $resultSet->GetColumnValue(4), AppServerName => $resultSet->GetColumnValue(5), State => $resultSet->GetColumnValue(6), URL => $resultSet->GetColumnValue(7), } while ($resultSet->MoveNext() == $CQPerlExt::CQ_SUCCESS) ; print row2html($_) foreach (@all_rows) ; # debug # use Data::Dumper ; # print "debug:\n", Dumper($_), "\n" foreach (@all_rows) ; sub row2html { my $row = shift ; return " <table border='1' cellpadding='8' bgcolor='#CCD6F5'> <tr> <td><input type='checkbox' value='$row->{VLSName}'/></td> <td>$row->{Release}</td> <td>$row->{Environment}</td> <td width='160'>$row->{Project}</td> <td width='430'><a href='$row->{URL}'>$row->{URL}</a></td> </tr> </table> " ; }


Comment on Re^3: Capture output of a while loop into a variable
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-09-17 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls