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

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?

Replies are listed 'Best First'.
Re^4: substitution in regular experssion
by aeqr (Novice) on Apr 23, 2014 at 20:21 UTC
    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.

        It is not a assignment we have to hand in, it is an optional task and the coding language is free to choose. But anyway, thanks for trying to help and for the nice comments.
          A reply falls below the community's threshold of quality. You may see it by logging in.

      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.