Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: regexp and substitute operator problem

by blokhead (Monsignor)
on Jul 08, 2003 at 06:18 UTC ( [id://272206]=note: print w/replies, xml ) Need Help??


in reply to regexp and substitute operator problem

There's a problem in the loop that removes items according to a regex. Whenever an item is removed, the items to the right are shifted over, but $i is still incremented. See:
my @arr = qw/ foo foozle bar bar2 foo bar foo /; for (my $i=0; $i<@arr; $i++) { splice(@arr, $i, 1) if ($arr[$i] =~ /bar/); } print "$_\n" for (@arr); ## output: bar2 is NOT removed
It will fail to check the next item after each match. It's much simpler (and correct-er) to write:
@chroms = grep { ! /[pqxy]/i } @chroms;
Although, if you're dealing with a huge amount of genetic data, this will be very slow. A quick fix to the existing loop would be the slight change:
for (my $i=0; $i<@chroms; $i++) { splice(@chroms, $i--, 1) if not $chroms[$i] =~ /[pqxy]/i; } ####
This still may not be optimal, but it's in-place so should be faster than the grep.

About your second question... Off the top of my head, I'm not sure what's up with your regex... It's late for me!

blokhead

Replies are listed 'Best First'.
Re: Re: regexp and substitute operator problem
by dannoura (Pilgrim) on Jul 08, 2003 at 06:28 UTC

    Yep, you're right, that was a pretty stupid mistake. Thanks for your help. This:

    @chroms = grep {  /[pqxy]/i } @chroms;

    solves it.

      As for your second question, just copy/pasting your code and the example text does output the hyphens and dots here. What are you displaying it on?

      Also, you might at least want to put some () around the central part of your regex, as in: /\s($c*?)\s/ so you won't capture the whitespace around the matches as you do now. Or maybe that is what you wanted.


      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://272206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-16 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found