Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^6: Using pos() inside regexp (no /e)

by braveghost (Acolyte)
on Oct 09, 2010 at 09:08 UTC ( [id://864348]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Using pos() inside regexp (no /e)
in thread Using pos() inside regexp

I'm sorry, but how that can help me?

I've read 1 and 3 points of your list, and just take a look at second point. They don't contain answers on my questions, which is:

1. Is it possible to set pos() value from regexp itself, and how to do it?
2. How can \G assertion help me to change pos() value?

Look like you have misunderstand me.
perldoc -f pos pos SCALAR pos Returns the offset of where the last "m//g" search left + off for the variable in question ($_ is used when the variable +is not specified). Note that 0 is a valid match offset. "und +ef" indicates that the search position is reset (usually du +e to match failure, but can also be because no match has yet + been performed on the scalar). "pos" directly accesses the l +ocation used by the regexp engine to store the offset, so assig +ning to "pos" will change that offset, and so will also influen +ce the "\G" zero-width assertion in regular expressions. Becau +se a failed "m//gc" match doesn't reset the offset, the retu +rn from "pos" won't change either in this case. See perlre and + perlop.
Point is “"pos" directly accesses the location used by the regexp engine to store the offset” and I want to change it.
Second point is
perl -e "\$_='qwerty';s/r/print pos()/e;"
so I can read pos value, and it is logical that I can set it too. But I don't know how to do it, and can't find any examples.

Now about \G which says regexp to start next search from the end of previous. It's main purpose to check several regexp on string, special for that its used with c modifier practically all the time. But in my case all actions make in function which called by regexp. But after then function change string, new search must start from earlier position then pos() value. And I don't understand how can \G help me?
Of course I can miss something, but what?
I'm sorry for my language level, i can't says clearly what exactly I mean. But hope you will understand.

Replies are listed 'Best First'.
Re^7: Using pos() inside regexp (no /e)
by ww (Archbishop) on Oct 09, 2010 at 11:46 UTC
    I wonder if perhaps you're asking about something like this:
    #!/usr/bin/perl use strict; use warnings; # 864348 $_='qwerty erk'; my $pos; my $foo = $_ =~ s/r/ $pos=pos(). "\n"; print pos(); print "\n"; ++( pos() ); print "\t" . pos() . "\n"; print($pos . "\n")/gex; print "Now, outside the regex, $pos";

    Output:

    3 4 3 8 9 8 Now, outside the regex, 8

    (Note: as you wrote the one-liner in your previous, you wrote an illegal ref to $_; the $ does NOT need escaping in your one-liner.
    ... and I'm confused: "G" and "g" come and go in your posts... sometimes one; sometimes the other; and sometimes, not at all.)

      Yes! This is exactly the thing i talk about, pos() for next iteration of regexp is not changes. And looks like there is no way to change it, and this is realy meaningless because variable regexp save initial value of variable and use this value for search and another for replace results.
      $ perl -MDevel::Peek -e "\$_='qwerty';Dump(\$_);s/[rt]/Dump(\$_);/ge; +print Dump(\$_)" SV = PV(0x801838) at 0x82fde0 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = 3 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x201fe0 "qwerty"\0 CUR = 6 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = 4 SV = PVMG(0x823514) at 0x82fde0 REFCNT = 1 FLAGS = (SMG,POK,pPOK) IV = 0 NV = 0 PV = 0x2010d0 "qwey"\0 CUR = 4 LEN = 8 MAGIC = 0x2018d0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_LEN = -1
      So, it seems my task can't be solved by using follow construction
      $data =~s/([^\n]{16})/pos($data)=0;ch($1)/ge;
      (Note: escaping of $ simbol is nessesary for bash
      # perl -e "\$_='qwerty';s/r/print pos();/e;" 3 BUT # perl -e "$_='qwerty';s/r/print pos();/e;" syntax error at -e line 1, near ";=" Execution of -e aborted due to compilation errors. # uname -v Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1 +504.7.4~1/RELEASE_I386
      )

        Take your "BUT" example and extend it:

        perl -e "$_='qwerty'; s/r/print pos(). \"\n\";/e; print $_;

        OUTPUT:

        3 qwe1ty

        Note the "1" (one) in the replaced output. Maybe you can go somewhere with this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-16 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found