Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Concise code to list array elements

by tel2 (Pilgrim)
on Apr 08, 2013 at 03:05 UTC ( [id://1027414]=perlquestion: print w/replies, xml ) Need Help??

tel2 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there a concise way of writing this?:

  $sth->execute($role[$row][1], $role[$row][2], $role[$row][3], $role[$row][4], $role[$row][5], $role[$row][6], $role[$row][7], $role[$row][8], $role[$row][9], $role[$row][10]);

I tried this:

  $sth->execute($role[$row][1..10];

but that doesn't seem to work.  I assume that's because the latter is a slice?

Thanks.
tel2

Replies are listed 'Best First'.
Re: Concise code to list array elements
by Gangabass (Vicar) on Apr 08, 2013 at 03:18 UTC
    $sth->execute( @{ $role[$row] }[1..10] );
      Thanks heaps, Gangabass!  Exactly what I need.
Re: Concise code to list array elements
by Loops (Curate) on Apr 08, 2013 at 03:48 UTC
    Hi,

    Your sigil is wrong to take an array slice. You'll need something like:

    my @role = ( [ qw(a b c d e f g h i j k l m) ], [ qw(n o p q r s t u v w x y z) ], ); my $row = 0; say $role[$row][1], $role[$row][2], $role[$row][3], $role[$row][4], $r +ole[$row][5], $role[$row][6], $role[$row][7], $role[$row][8], $role[$ +row][9], $role[$row][10]; say @{$role[$row]}[1..10];

    Also, make sure that you really want to skip the first element of the row in your example. Array indexes start at 0, not 1.

      Thanks for that pointer, Loops.

      BTW, what does 'sigil' mean in this context?

        In a Perlish context, a sigil is the $, @, %, or & that precedes an identifier.

        Although it doesn't actually use the sigil word, perldata will help to understand the syntax of slices.


        Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-28 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found