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

Re: Capture output of a while loop into a variable

by onelesd (Pilgrim)
on Sep 26, 2011 at 05:24 UTC ( #927794=note: print w/ replies, xml ) Need Help??


in reply to Capture output of a while loop into a variable

There's quite a few ways to skin this cat, some of them better than doing it in the while loop, but since you said you want to do it in your while loop, you can do that this way without changing your existing code:

my @save4later ; while ($status == $CQPerlExt::CQ_SUCCESS) { my $Environment = $resultSet->GetColumnValue(1); my $Release = $resultSet->GetColumnValue(2); my $Project = $resultSet->GetColumnValue(3); my $VLS_Name = $resultSet->GetColumnValue(4); my $AppServerName = $resultSet->GetColumnValue(5); my $State = $resultSet->GetColumnValue(6); my $URL = $resultSet->GetColumnValue(7); push @save4later, { Environment => $Environment, Release => $Release, Project => $Project, VLS_Name => $VLS_Name, AppServerName => $AppServerName, State => $State, URL => $URL, } ; print <<END_HTML; <table border="1" cellpadding="8" bgcolor="#CCD6F5"> <tr> <td><input type="checkbox" value="$VLSName"/></td> <td>$Release</td> <td>$Environment</td> <td width="160">$Project</td> <td width="430"><a href="$URL">$URL</a></td> </tr> </table> END_HTML # next row in result set $status = $resultSet->MoveNext; } use Data::Dumper; print Dumper(@save4later);


Comment on Re: Capture output of a while loop into a variable
Download Code
Re^2: Capture output of a while loop into a variable
by premal (Acolyte) on Sep 27, 2011 at 08:34 UTC

    Thanks a lot for your reply. You said in your comment that there are better ways of doing this without using while loop. I will highly appriciate if you can provide me better looping structure or solution where in I can retrive everything from ClearQuest and store it in a variable. Later I cau user that variable and all values for further processing.

      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> " ; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-30 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls