Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how many lines returned from backticks

by jdpindynet (Initiate)
on Jun 30, 2002 at 02:40 UTC ( #178297=perlquestion: print w/ replies, xml ) Need Help??
jdpindynet has asked for the wisdom of the Perl Monks concerning the following question:

Oh holy ones ... this knave is trying to use backticks repeatedly with a program that returns a variable number of lines. How can I tell how many lines I got?
my @lines = `findgsc $ra$sign$dec -rs 6 -p 2 -n 10 `; if ($#lines >= 3) { do stuff; } else { do other stuff }
Which seems to work fine, the first time around. What do the Monks think about using $#lines on the next time around? If the program returned 5 lines the first time, and only 2 the next, won't there be lines left over from the first time? How can I tell how many _additional_ lines I got back? Awaiting your wisdom. jdpindynet

Comment on how many lines returned from backticks
Download Code
Re: how many lines returned from backticks
by little (Curate) on Jun 30, 2002 at 02:47 UTC
    try
    # in scalar context an array # will return the number of its elements print scalar @lines; #which leads to if (scalar @lines) { #array is not (an) empty (list) ... }
    Regarding your question there is no need to worry if th earray gets newly populated each time, but if not in a block (especially loop) you might avoid such case by simply resetting the array with "@lines = ();".

    Have a nice day
    All decision is left to your taste
Re: how many lines returned from backticks
by dws (Chancellor) on Jun 30, 2002 at 03:56 UTC
    If the program returned 5 lines the first time, and only 2 the next, won't there be lines left over from the first time? How can I tell how many _additional_ lines I got back?

    This rather depends on how you have things scoped. If the backticks are in a subroutine that gets called multiple times, each invocation of the routine will get a new array if the backticks line is coded as:   my @lines = `findgsc ...`;
    If you move the declaration of @lines higher in the script, and then do   @lines = `findgsc ...`;
    multiple times, each one overwrites @lines with new values. To get a delta on line count (i.e., note additional lines), merely save the prior count. Conceptually,

    @lines = `finedgsc ...`; $prevLines = scalar @lines; @lines = `findgsc ...`; if ( scalar @lines > $prevLines ) { # we got more this time }
    Note that scalar @lines is the absolute line count, while $#lines is the final index in the array, which will be one less than the absolute count. Which to use is a matter of style, but don't confuse the two.

Re: how many lines returned from backticks
by Aristotle (Chancellor) on Jun 30, 2002 at 10:33 UTC
    If the program returned 5 lines the first time, and only 2 the next, won't there be lines left over from the first time?

    Actually, despite what others here may have said, you do not have to worry about anything. Unlike traditional languages, an array in Perl is not just a name for a series of consecutive memory addresses (just like scalars are not just names for single memory addresses); it is a smart data type (again, scalars are too) which may grow and shrink, moving about in memory as circumstances demand etc. It does what traditional languages require you to implement linked lists for, and more.

    The whole point of this explanation:
    my @a=(1,2,3,4,5); @a=(1,2,3); print "@a\n"; =output 1 2 3
    Perl does the nitty gritty of keeping things clean for you. Garbage collection, undef, references being smart unlike pointers, and so and so forth..

    Makeshifts last the longest.

      Many thanks to all who answered, especially Aristotle.

      The knave has now lost one measure of knaveness, thanks to the wisdom of the monks.

      jdpindynet

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (17)
As of 2014-10-24 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls