Solved

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

Posted on 2003-11-29
6
178 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
  • 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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