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