Re: substr forces scalar context with array argument



Andrew wrote:
> In the code below, the array '@tmp' with two elements is treated as its
> scalar (the number 2), when used as argument to substr. When I
> syntactically break up '@tmp' into '$tmp[0]' and '$tmp[1]', substr does
> what i want. Why is this so? Is there a way to force list context
> (besides explicit enumeration)? I can't seem to find any mention of
> this anywhere.

$ perldoc -f substr
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET

substr is expecting between 2 and 4 scalars. So when you pass it an
array, yes, it converts that array to a scalar. That's what it's
documented to do.

> Could it be an oversight/bug in substr? (probably not,
> given the thoroughness that has gone into the development of perl
> functions, but this is unperl-like limitation :-)

I don't see it as a limitation at all. How frequently are you going to
have such distinct values as an offset and a length contained in a
single array? Unless you do so very intentionally, it's just not
likely to happen.

Paul Lalli

.



Relevant Pages

  • chinese and arrays
    ... beginner level script, with just functions. ... taking into an array the text substr took ... I think it's no longer chinese ...
    (comp.lang.php)
  • substr forces scalar context with array argument
    ... the array '@tmp' with two elements is treated as its ... scalar, when used as argument to substr. ... (Also, FWIW, using '@tmp' yields a weird result I can't explain) ...
    (comp.lang.perl.misc)
  • Re: Text Manipulation help needed
    ... u> my @array = qw; ... perldoc -f splice for more. ... think) substr added a 4th arg to match splice's 4th arg. ...
    (perl.beginners)
  • Re: split n characters into n chunks
    ... you can use substr $myword, 1,3 function ... my $word = "thequickbrown" ... string and push each character into array, then loop through the array, ...
    (perl.beginners)
  • process takes a long- BULK COLLECT
    ... TYPE ARRAY IS TABLE OF VARCHAR2 ... SUBSTR AS minutos, ... SUBSTR AS segundos, ... (cliente, segmento, servicio, ...
    (comp.databases.oracle.server)