Re: substr forces scalar context with array argument



> > 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.

Yes, I had looked at "perldoc -f substr" before posting. Au contraire,
the fact that "substr is expecting between 2 and 4 scalars", as you put
it, is NOT documented. The word 'scalar' is nowhere to be found in the
doc.

Furthermore, I suppose we are running into the issue of semantics
"versus" syntax, and the delineation between the two. What a perl list
means "semantically" (conceptually) may be something different from
what it is syntactically.

The documentation bit "substr EXPR,OFFSET,LENGTH,REPLACEMENT" certainly
suggests a list. And, in as much as an @array is a (semantic?)
"representation" of multiple scalars, i may be misled to substitute
this representation for that which it represents.

In ordinary subroutines that i build, "($x,$y)" and "@tmp" ( where
@tmp=($x,$y) ) are certainly interchangeable as arguments to the
subroutine (grabbed via @_ or $_[0], $_[1], ..., etc., within the sub).
Using either yields exact same results.

Thus, it appears to me, that the "substr" function does something out
of the norm with the arguments, distinguishing between and acting
differently with the two SYNTACTICAL variations (of that which is
semantically equivalent)


>> 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.
>

Real-life example: I have to extract substrings of different offsets
and lengths (with many iterations). I define my offsets and lengths in
a List of Lists:

@LoL=( [1,4], [5,3], [10,3], [54, 5], [9,2], .... etc. );

Then (for every iteration) i want to grab my multiple substrings like
so:

@substrings=map { substr($string, @{$LoL[$_]}) } (0..$#LoL);

OK, i realize, i can "fix" this by adding more code:

@substrings=map { substr($string, $LoL[$_][0], $LoL[$_][1] ) }
(0..$#LoL);

But what if my offsets and lengths where in a List of Hashes of Lists
of Lists? :-)
And, the principle of it... syntactical brevity... which, in my
experience, has always been a hallmark of Perl...particularly vis a vis
arrays/hashes/scalars.

andrew

.



Relevant Pages

  • Re: substr forces scalar context with array argument
    ... >>Because substr has a prototype demanding scalars. ... array to specify the offset or length. ... > mention either lists or scalars, which may leave the reader guessing. ...
    (comp.lang.perl.misc)
  • Re: substr forces scalar context with array argument
    ... when used as argument to substr. ... Is there a reason the prototype is demanding scalars? ... mention either lists or scalars, which may leave the reader guessing. ...
    (comp.lang.perl.misc)
  • Re: 20050111: list basics
    ... ># it tells perl that it is a list. ... ># perl can't print lists. ...
    (comp.lang.perl.misc)
  • Re: grid structures: TABLE/TR/TD vs. DIV
    ... it did not work (not same semantics ... ... The content producer (that does not produce the CSS) decides how ... Like lists are inside tables ... |> My "semantics of a list" does not require a bullet. ...
    (comp.infosystems.www.authoring.stylesheets)
  • Re: Why cons *pairs*?
    ... is there a GOOD REASON why lists should be the ... "interesting" semantics to vectors rather than (or perhaps in addition ... trivial isomorphism to CONS, CAR, and CDR, or that the additional ...
    (comp.lang.scheme)