in reply to Re: substitution in regular experssion
in thread substitution in regular expression

Thanks for the info, I have tried your solution but it doesn't seem to work :/

Also I would like to know the way to do it without the while loop. That is, editing the string and saving as I have described...

  • Comment on Re^2: substitution in regular experssion

Replies are listed 'Best First'.
Re^3: substitution in regular experssion
by AnomalousMonk (Archbishop) on Apr 23, 2014 at 20:13 UTC
    ... it doesn't seem to work :/

    But what does that mean? In general, replies along the lines of "it doesn't work" are not helpful. How does it "not work"?

Re^3: substitution in regular experssion
by Anonymous Monk on Apr 23, 2014 at 20:14 UTC

    It seems you've edited your node to remove the while loop you originally had. Please don't do that without marking your updates because it confuses things, now monks won't know which version of your question to answer.

    ... it doesn't seem to work

    In what way? Do you get an error, or are you seeing unexpected results? Because it works for me:

    use Data::Dumper; print Dumper([build_dictionnary()]); sub build_dictionnary{ my $line="ABCDEF"; my @dic; while(length($line)>2){ $line =~ s/([A-Z]([A-Z]{2}))/$2/; push(@dic, $1); } return @dic; } # Output (whitespace compressed): # $VAR1 = [ 'ABC', 'BCD', 'CDE', 'DEF' ];
    I would like to know the way to do it without the while loop.

    Why?

      Sorry I made a mistake, yes it works indeed thanks. I wanted to do it without the while in order to train myself to use the global option and in order to do it in one line. Maybe not very useful but in my assignment it is recommended to find short solutions.
        ... in my assignment ...

        Changing your posts without citation and not being up-front about posting homework questions is a good way to earn lotsa down-votes. Good luck in your studies, and remember that teachers nowadays have lots of sneaky ways to discover sneaky students sneaking answers off of the Interwebs.

        Okay, that's a legitimate reason, although it's considered polite to let people know that you're asking for assignment help in the original post.

        AnomalousMonk has shown a way to do it in one line, although that solution doesn't modify the string.

        If you want to evaluate code on the right-hand side of your search/replace regular expression, see the /e modifier in perlop.