Solved

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

Posted on 2003-11-29
6
175 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kaweh
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
OTRS Installation 1 276
Add additional column to .csv using Perl. 8 120
batch script for automated email 12 94
Perl string filter 5 61
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now