Re: substr forces scalar context with array argument



Andrew wrote:
> #-------------- output -------------------
>
> substr($s, $offset, $length) ---> acad
> substr($s, @tmp) ---> racadabra
> substr($s, @tmp[0,1]) ---> cadabra
> substr($s, $tmp[0], $tmp[1]) ---> acad
>
> #------------ end output ----------------
>
> ( Also, FWIW, using '@tmp[0,1]' yields a weird result I can't explain )

Didn't see this comment originally. The issue here is that a list
slice returns a list, not an array. A list in scalar context returns
the last item of that list. Therefore,
substr($s, @tmp[0,1]);
is equivalent to:
substr($s, 4);
since $tmp[1] == 4.

Paul Lalli

.



Relevant Pages

  • Re: Difficulty with slice of referenced array
    ... or is there a correct way of coding the slice? ... but with a ref to @array, ... can't be a slice, there can only ever be one scalar in the ). ... in scalar context has the peculiarity that ...
    (comp.lang.perl.misc)
  • Slice to end of array
    ... Given an array @x, what is the most elegant way to get a slice from index $n to the end of the array? ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: Profile from an Intensity Graph?
    ... An intensity graph is just a 2D array. ... Simply slice out a column or row as desired. ... Prev by Date: ...
    (comp.lang.labview)
  • Re: BIGLOO vs FORTRAN
    ... multidimensional array if you wanna slice lets say: ... various dimensions and orders. ... (vector->list erg) ...
    (comp.lang.scheme)
  • Re: unsigned type
    ... says that for a slice, "If the slice is not a null slice (a slice ... that all index must be valid before checking for a null array. ... work with modular index types because the 'First of the string is the ...   ...
    (comp.lang.ada)