Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Suggestion on rewriting small code

by BillKSmith (Prior)
on Aug 21, 2019 at 10:55 UTC ( #11104801=note: print w/replies, xml ) Need Help??

in reply to Suggestion on rewriting small code

Try sprintf.
use strict; use warnings; my @arr = (12341,1245125,1525125,125125125); my $str = sprintf 'job==%d || 'x(@arr-1) . 'job==%d', @arr; print "$str\n";

Replies are listed 'Best First'.
Re^2: Suggestion on rewriting small code
by AnomalousMonk (Bishop) on Aug 21, 2019 at 15:21 UTC
    my $str = sprintf 'job==%d || 'x(@arr-1) . 'job==%d', @arr;

    ++ your solution, but just a minor point of curiosity: why use  @arr-1 instead of  $#arr here?

    Give a man a fish:  <%-{-{-{-<

      I have adopted the convention of using $#... when I actually mean "index of the last element" and @... when I mean a "count" of elements. A very small advantage is that this does not depend on the default value of "$[". (I have not used that in since I got over my FORTRAN habits years ago)
        ... when I mean a "count" of elements.

        Yeah, I guess the multiplier really is "one less than the number of array elements". Thank you for satisfying my curiosity.

        ... the default value of "$[".

        In Perl 5, changes | assignment to  $[ have | has been increasingly constrained over the years and in the most recent versions are | is completely disabled, but I still can't help thinking when I write something like  0 .. $#ra to list all possible indices of an array that I should perhaps really be writing  $[ .. $#ra instead.

        Give a man a fish:  <%-{-{-{-<

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2020-12-01 15:40 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (11 votes). Check out past polls.