How to use (??{code}) correctly in foreach loop?

Discussion in 'Perl Misc' started by Peng Yu, Feb 14, 2011.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I have the following code and output. I'm not sure why the code
    doesn't search for 'abc' in the second string 'efgabcabcd'. Could you
    anybody let me know what the problem is and how to fix it?

    $ cat main.pl
    #!/usr/bin/env perl

    use strict;
    use warnings;

    my @strings=('abcdefgabc', 'efgabcabcd');
    my $pattern='abc';

    foreach my $s (@strings) {
    my @offsets=();
    {
    use re 'eval';
    $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g;
    }
    print join('::', @offsets), "\n"
    }
    print "end\n";

    $ ./main.pl
    0::7

    end
     
    Peng Yu, Feb 14, 2011
    #1
    1. Advertising

  2. On 2011-02-14, Peng Yu <> wrote:
    > Hi,
    >
    > I have the following code and output. I'm not sure why the code
    > doesn't search for 'abc' in the second string 'efgabcabcd'. Could you
    > anybody let me know what the problem is and how to fix it?
    >
    > $ cat main.pl
    > #!/usr/bin/env perl
    >
    > use strict;
    > use warnings;
    >
    > my @strings=('abcdefgabc', 'efgabcabcd');
    > my $pattern='abc';
    >
    > foreach my $s (@strings) {
    > my @offsets=();
    > {
    > use re 'eval';
    > $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g;
    > }
    > print join('::', @offsets), "\n"
    > }
    > print "end\n";


    I have no idea what do you want to achieve. Do you think it is a
    scalar context //g, or a list content?

    Anyway, I suspect a bug in scalar/list context //g applied to a
    foreach-iterator. Did you try it without (??{}) ?

    Ilya
     
    Ilya Zakharevich, Feb 14, 2011
    #2
    1. Advertising

  3. "Peng Yu" <> wrote in message
    news:...
    > Hi,
    >
    > I have the following code and output. I'm not sure why the code
    > doesn't search for 'abc' in the second string 'efgabcabcd'. Could you
    > anybody let me know what the problem is and how to fix it?
    >
    > $ cat main.pl
    > #!/usr/bin/env perl
    >
    > use strict;
    > use warnings;
    >
    > my @strings=('abcdefgabc', 'efgabcabcd');
    > my $pattern='abc';
    >
    > foreach my $s (@strings) {
    > my @offsets=();
    > {
    > use re 'eval';
    > $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g;
    > }
    > print join('::', @offsets), "\n"
    > }
    > print "end\n";
    >
    > $ ./main.pl
    > 0::7
    >
    > end




    # Here is what you want
    # somehow the (??{..@offsets...}) defines the arrray as local at the
    namespace at first time
    # and "my" undefine it or something like that !

    use strict;
    use warnings;

    my @strings=('abcdefgabc', 'efgabcabcd');
    my $pattern='abc';
    my @offsets=();

    use re 'eval';
    foreach my $s (@strings)
    {
    $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g;
    print "Offsets of $s: @offsets\n"
    }
    no re;

    print "end\n";
     
    George Mpouras, Feb 14, 2011
    #3
  4. # sorry here is the correct one !

    use strict;
    use warnings;

    my @strings=('abcdefgabc', 'efgabcabcd');
    my $pattern='abc';
    my @offsets=();

    foreach my $s (@strings)
    {
    @offsets=();
    {use re 'eval'; $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g }
    print "Offsets of $s: @offsets\n"
    }

    print "end\n";
     
    George Mpouras, Feb 14, 2011
    #4
  5. Peng Yu

    Peng Yu Guest

    On Feb 14, 3:30 am, "George Mpouras"
    <> wrote:
    > # sorry here is the correct one !
    >
    > use strict;
    > use warnings;
    >
    > my @strings=('abcdefgabc', 'efgabcabcd');
    > my $pattern='abc';
    > my @offsets=();
    >
    > foreach my $s (@strings)
    > {
    > @offsets=();
    > {use re 'eval'; $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g }
    > print "Offsets of $s: @offsets\n"
    >
    > }
    >
    > print "end\n";


    This works. Thank you!

    Also, I don't quite understand why "my @offsets;" has to be moved out
    of the loop.

    I checked Programming Perl 3rd edition. There are thousands of "my" in
    this book. Would you please help me understand why it is the case by
    pointing me which page of Programming Perl or which perl document (and
    what part of it) I should read?
     
    Peng Yu, Feb 14, 2011
    #5
  6. >>>Also, I don't quite understand why "my @offsets;" has to be moved out of
    >>>the loop.


    There is nothing wrong with your code . It looks like a bug to me .
    (??{...@offsets ...}) } somehow defines the array as global at first time ,
    and "my" seems to overide this definition
     
    George Mpouras, Feb 14, 2011
    #6
  7. On 2011-02-14, Peng Yu <> wrote:
    >> my @offsets=();
    >>
    >> foreach my $s (@strings)
    >> {
    >> @offsets=();
    >> {use re 'eval'; $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g }
    >> print "Offsets of $s: @offsets\n"
    >>
    >> }


    > This works. Thank you!
    > Also, I don't quite understand why "my @offsets;" has to be moved out
    > of the loop.


    This one is new to me...

    When I first implemented (??{}), I did not cover one case in
    "dynamically create new chunk of compile tree" logic. This lead to
    bugs when (??{}) accesses lexicals in recursive subroutines. (Not
    hard to fix, but, apparently, nobody did it...)

    Now you say that there is also a problem with accessing lexicals
    defined inside a loop? Strange... Probably, the logic of the first
    loop may be as in:

    A lexical @array is created;

    REx is compiled; the compile tree contains a reference to the @array;

    On exit from the loop the garbage collector of lexicals checks the
    refcount of @array; it is now 1 more than expected, so it thinks an
    EXTERNAL reference to the array exists, and "renews" @array;

    On following iterations "there is no visible need" to renew @array
    and recompile the REx, so none of these steps happens. However, on
    these iterations @array visible from Perl code and @array visible from
    inside (??{}) are different containers.

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Feb 15, 2011
    #7
  8. Peng Yu

    C.DeRykus Guest

    On Feb 13, 6:01 pm, Peng Yu <> wrote:
    > Hi,
    >
    > I have the following code and output. I'm not sure why the code
    > doesn't search for 'abc' in the second string 'efgabcabcd'. Could you
    > anybody let me know what the problem is and how to fix it?
    >
    > $ cat main.pl
    > #!/usr/bin/env perl
    >
    > use strict;
    > use warnings;
    >
    > my @strings=('abcdefgabc', 'efgabcabcd');
    > my $pattern='abc';
    >
    > foreach my $s (@strings) {
    >   my @offsets=();
    >   {
    >     use re 'eval';
    >     $s =~ /(?=\Q$pattern\E)(??{push @offsets, $-[0]})/g;
    >   }
    >   print join('::', @offsets), "\n"}
    >
    > print "end\n";
    >
    > $ ./main.pl
    > 0::7
    >
    > end


    Is there some reason you need to use (?{{...})
    which is tagged 'experimental' ...

    A simple alternative, eg:

    my $regex = qr/abc/;
    foreach my $s (@strings) {
    push @offsets,$-[0] while $s =~ /$regex/g;
    }

    --
    Charles DeRykus
     
    C.DeRykus, Feb 15, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    380
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    325
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    744
    Scott Allen
    Feb 12, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page