Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Is there a module for object-oriented substring handling/substitution?

by choroba (Chancellor)
on Jan 24, 2013 at 23:21 UTC ( #1015244=note: print w/replies, xml ) Need Help??


in reply to Is there a module for object-oriented substring handling/substitution?

Do you know that a reference to substr works as you described?
my $string = 'abcdefghijklmnopqrstuvwxyz'; my $substring = \substr $string, 10, 10; $$substring = uc $$substring; print $string, "\n";
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: Is there a module for object-oriented substring handling/substitution?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there a module for object-oriented substring handling/substitution? (2 substrs)
by tye (Sage) on Jan 25, 2013 at 01:57 UTC
    Do you know that a reference to substr works as you described?

    Only if you use a single substring. I have a partially-written module that does this type of thing and supports multiple simultaneous substrings of the same string such that they cooperate (which leads to some tricky bits which were fun to hash out).

    But tye having an unpublished module isn't really a reason to avoid writing one's own version of something similar.

    - tye        

      They long since lifted the 'only one lvalue ref' limitation. This is 5.10.1:

      $s = 'abcdefghijklmnopqrstuvwxyz';; @r = map \substr( $s, $_*4, 2), 0..6;; $$_ = uc $$_ for @r;; say $s;; ABcdEFghIJklMNopQRstUVwxYZ say $];; 5.010001

      It does have its limitations though. (Unsurprisingly) The lvalue refs do not adjust to accommodate replacements that alter the length of the string:

      $$_ = $_ for @r;; say $s;; REF(REF(REF(REF(REF(REF(REF(0x3e82050)3e821e8)3e82260)3e820e0)11c458)3 +e820c8)335dc0)cdEFghIJklMNopQRstUVwxYZ

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        [ Update: The end of the post I replied to, starting at "It does have its limitations though" was not present when I replied (nor during the several times I had reason to load a page that included that node during the time I took composing my reply -- I don't believe it was even present immediately after I replied; though I am less sure of that). FYI. ]

        Do you know that a reference to substr works as you described?
        Only if you use a single substring.
        They long since fixed the 'only one lvalue ref' limitation.

        Thanks for the notice, but that isn't what I was talking about. Being able to have more than one lvalue substr ref doesn't itself result in "multiple simultaneous substrings of the same string such that they cooperate" nor to them "working as the OP described". (Nor does it lead to any particularly "tricky bits", certainly not any that qualify as "fun to hash out".)

        #!/usr/bin/perl -w use strict; my $s = 'one,two,three,four'; my @p; while( $s =~ /([^,]+)/g ) { push @p, \substr( $s, pos($s)-length($1), length($1) ); } print "($$_)" for @p; print " [$s]\n"; ${$p[0]} = 'five'; # Change 'one' to 'five' ${$p[2]} = 'X'; # Change 'three' to 'X' print "($$_)" for @p; print " [$s]\n"; print $], $/; __END__ (one)(two)(three)(four) [one,two,three,four] (five)(,tw)(X)(r) [five,twoXe,four] 5.012000

        While the OP was requesting an outcome like:

        (five)(two)(X)(four) [five,two,X,four]

        Note that I'm not trying to argue that \substr magic should be taught to "cooperate". What \substr magic does is quite simple and sane and I see no reason to change it. It just doesn't match what the OP was asking for (nor what my module implements).

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015244]
help
Chatterbox?
[Corion]: That is easy without having to pay for a house, a wife or children though. If I had any of these, or any two of these, the decision wouldn't be that easy.
[ambrus]: wait. I understand no wife and children, but how do you not have to pay for a house?
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-09-21 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?