why I can not use $$i when I used strict?

If I don't use strict it works. But after I used strict it does't work.
$i == 1 to i, I use $$i is because I want to get all the matched sequences stored in $1, $2........$$i

Is there anyway that I can avoid these problem when I still use strict?

thanks a lot!
kawehAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
The construct $$i that you are using is what is considered a "soft reference", something that is forbidden by "use strict".

The primary way to get the captured matches stored into variables is to do an assignment. For example,

my ( $left, $middle, $right) = $string =~ /^(\S+)\s(.+)\s(\S+)$/;

Does almost exactly the same thing as the two statements:

$string =~ /^(S+)\s(.+)\s(\S+)$/;
my ($left, $middle, $right) = ( $1, $2, $3);

You can also do the assignment to an array, which is sometimes more convenient, especially when you do not know in advance how many matches will occur.

my (@matches) = $string =~ /(\d+)/g;

0
kawehAuthor Commented:
jmcg, thank you very much!

Actually, I want to do global match but it seems the last example doesn't work.

for example if my $string = "ADCDGCEDCDGHNF";
@match = $string =~ /(\w{2,3})CD(\w{2,3})/g;
 only AD and GCE is in @match.

the another thing is, when I do global match, I don't want to push all the matched parts into @match at once. for instance, the above example has two matched pattern, (AD, GCE) and (CED, GH). I don't want to push them all together into @match.  That's because I need to deal with them separately. So can I use a while function do the global match:  while($string =~ /(\w{2,3})CD(\w{2,3})/g ){ push the matched part into @match }.  Actually, I don't know how many () in my pattern, here I just give an example. If I don't know the number, how can I get the captured match here?

Thank you very much!
0
jmcgOwnerCommented:
The reason you get only the first pair of matches is because the matches overlap. Handling overlapping matches can be hard. For this particular case, the best way I can think of to handle it is to use a lookahead assertion so the match on the right hand side of the CD is not actually "eaten" by the match.

my $string = "ADCDGCEDCDGHNF";

@match = $string =~ /(\w{2,3})CD(?=(\w{2,3}))/g;

use Data::Dumper;
print Dumper( \@match);
===================
This gives, as results:

$VAR1 = [
          'AD',
          'GCE',
          'CED',
          'GHN'
        ];

===================
Now, you know how many grouping parens you have in your match expression, so you can process these results a set at a time by doing something like:

while( my ($left, $right) = splice( @match, 0, 2)) {
    # the pair $left and $right correspond to the matches around the Nth CD
    # you can deal with them pairwise here
    }
===================


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

kawehAuthor Commented:
Great! Thanks!

one thing I feel confused is why you use Data::Dumper here?
@match can be printed out directly.
0
jmcgOwnerCommented:
Yes, it certainly can. The format of Data::Dumper output makes it very easy to see boundaries between strings and lets me print out data structures without thinking about them. I therefore reach for it almost automatically even when, with a little thought, I could do something simpler. I'm sorry its needless introduction caused some confusion.
0
kawehAuthor Commented:
oh,no, it's great for me to learn more.
That's a great idea to use this format because I know how bad if I don't use it.
In my project there are lots of array of array such kind of structure.
Actually, I learned a lot here. Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.