Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Interchanging hash values

by Abigail-II (Bishop)
on Apr 20, 2003 at 01:28 UTC ( [id://251752]=note: print w/replies, xml ) Need Help??


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 (Patriarch) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://251752]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found