Solved

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

Posted on 2003-11-29
6
181 Views
Last Modified: 2010-03-04
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!
0
Comment
Question by:kaweh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9844580
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
 

Author Comment

by:kaweh
ID: 9847298
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
 
LVL 20

Accepted Solution

by:
jmcg earned 30 total points
ID: 9847409
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kaweh
ID: 9847822
Great! Thanks!

one thing I feel confused is why you use Data::Dumper here?
@match can be printed out directly.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9847896
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
 

Author Comment

by:kaweh
ID: 9848116
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question