in reply to Re: Re: Re: Interchanging hash values
in thread Interchanging hash values

sub insert_end_relative { my ($string, $pos, $insert) = @_; substr ($string, $pos || length $string) = $insert }

Abigail

Replies are listed 'Best First'.
Re: Re: Interchanging hash values
by BrowserUk (Pope) on Apr 20, 2003 at 01:38 UTC

    With a couple of additions, that works:)

    sub insert_end_relative { my ($string, $pos, $insert) = @_; substr ($string, (-$pos) || length $string, 0) = $insert }

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      You are right about the negation of $pos, but adding a third argument of 0 to substr gets you something else than leaving it off. If you just want to insert instead of replace (which your original code was doing), you don't even need the or:
      substr ($string, length ($string) - $pos, 0, $insert)

      will do.

      Abigail

        Whoops! {blush} Your right. I omitted the 0 in my code too. , but it was called insert_end_relative() :)

        Yes! That does do the trick, at a (miniscule) cost. I still like the idea, but it's just a transient wish.


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.