in reply to Re: An Oddity of substr
in thread An Oddity of substr

The character from the unaffected part of the original value isn't random. It's actually very determinate, as you can see from the below example.
$s='foobarstuv'; foreach $n (3 .. 5) { for $v (qw[ a ab abc abcd abcde abcdef ]) { $d = substr( $c=$s, 1, $n, $v); $f = substr( $e=$s, 1, $n) = $v; printf "4-arg: '%13s' (%5s) ... 3-arg: '%13s' (%5s)\n", $c, $d, +$e, $f; } print $/; } -------------- 4-arg: ' faarstuv' ( oob) ... 3-arg: ' faarstuv' ( aar) 4-arg: ' fabarstuv' ( oob) ... 3-arg: ' fabarstuv' ( aba) 4-arg: ' fabcarstuv' ( oob) ... 3-arg: ' fabcarstuv' ( abc) 4-arg: ' fabcdarstuv' ( oob) ... 3-arg: ' fabcdarstuv' ( abc) 4-arg: ' fabcdearstuv' ( oob) ... 3-arg: ' fabcdearstuv' ( abc) 4-arg: 'fabcdefarstuv' ( oob) ... 3-arg: 'fabcdefarstuv' ( abc) 4-arg: ' farstuv' ( ooba) ... 3-arg: ' farstuv' ( arst) 4-arg: ' fabrstuv' ( ooba) ... 3-arg: ' fabrstuv' ( abrs) 4-arg: ' fabcrstuv' ( ooba) ... 3-arg: ' fabcrstuv' ( abcr) 4-arg: ' fabcdrstuv' ( ooba) ... 3-arg: ' fabcdrstuv' ( abcd) 4-arg: ' fabcderstuv' ( ooba) ... 3-arg: ' fabcderstuv' ( abcd) 4-arg: ' fabcdefrstuv' ( ooba) ... 3-arg: ' fabcdefrstuv' ( abcd) 4-arg: ' fastuv' (oobar) ... 3-arg: ' fastuv' (astuv) 4-arg: ' fabstuv' (oobar) ... 3-arg: ' fabstuv' (abstu) 4-arg: ' fabcstuv' (oobar) ... 3-arg: ' fabcstuv' (abcst) 4-arg: ' fabcdstuv' (oobar) ... 3-arg: ' fabcdstuv' (abcds) 4-arg: ' fabcdestuv' (oobar) ... 3-arg: ' fabcdestuv' (abcde) 4-arg: ' fabcdefstuv' (oobar) ... 3-arg: ' fabcdefstuv' (abcde)

The extra characters are the characters from the string after substitution, up to the length of the substitution. So, the behavior is quite determinate. As well, it's very consistent. It's just a little counter-intuitive. *shrugs*

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re2: An Oddity of substr
by BrowserUk (Patriarch) on Oct 10, 2003 at 22:17 UTC

    Well damn! It's so obvious, I should'a recognised the pattern straight away. The left half of my brain musta been asleep:)

    Perhap between us we could come up with a nice simply, generic description for inclusion in the docs?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail