in reply to CGI.pm - multiple values for same field name

$INPUT->param("titles")
returns a list. You can't index a list, but you can slice it.
($INPUT->param("titles"))[5]
c.f. perldata

@{$INPUT->param("titles")}[5]
and
$INPUT->param("titles")->[5]
don't work because param doesn't return a reference to an array.

$INPUT->param("titles")[5]
it just plain invalid syntax.

Replies are listed 'Best First'.
Re^2: CGI.pm - multiple values for same field name
by shmem (Chancellor) on May 25, 2007 at 08:58 UTC
    Nit -
    $INPUT->param("titles")
    returns a list. You can't index a list, but you can slice it.
    ($INPUT->param("titles"))[5]

    Sure you can - perldata uses subscript instead of index:

    A common way to access an array or a hash is one scalar element at a time. You can also subscript a list to get a single element from it.

    You can't subscript a function call, though, only its results - that's why the expression must go in parens. But you are right in that you can also slice a list :-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      A common way to access an array or a hash is one scalar element at a time. You can also subscript a list to get a single element from it.

      And by doing so, you get a list slice. A single-index slice is still a slice. The difference between a slice and indexing is the context in which the index expression is evaluated.

      my @indexes = (3); my @array = (0,1,2,3,4); print($array[@indexes], "\n"); # 1 => index print(@array[@indexes], "\n"); # 3 => slice print((0,1,2,3,4,5)[@indexes], "\n"); # 3 => slice

      There's no native syntax to index a list.

        The difference between a slice and indexing is the context in which the index expression is evaluated.

        I see. That's why this sentence is found under Slices in perldata. Learnt something new today, thank you.

        update: looks like a statement like this one deserves downvoting for some. *shrug*

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}