Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by ww (Archbishop)
on Oct 09, 2010 at 11:46 UTC ( [id://864364]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^8: Using pos() inside regexp (no /e)
by braveghost (Acolyte) on Oct 10, 2010 at 15:58 UTC
    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.

        Again, escape of '$' is necessary for bash shell which used at unix-like system
        perl -e "$_='qwerty'; s/r/print pos(). \"\n\";/e; print $_;" syntax error at -e line 2, near "=" Execution of -e aborted due to compilation errors.
        but then $ is escaped
        perl -e "\$_='qwerty'; s/r/print pos(). \"\n\";/e; print \$_;" 3 qwe1ty
        It seems you try your example under windows, and it is work fine there, but i'm macos user. 1(one) is returned by print function and s/// insert it because it is last return at executed section, this is clear, but it can be easily fix
        perl -e "\$_='qwerty'; s/r/print pos(). \"\n\";\$&/e; print \$_;" 3 qwerty

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-18 00:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found