[Israel.pm] Fwd: [Telux] Next Meeting: "High-Level Programming Concepts Using Perl 6" on 22-March

Shlomi Fish shlomif at iglu.org.il
Sat Mar 14 05:25:55 PDT 2009

On Saturday 14 March 2009 01:28:46 Evgeny wrote:
> Oh, one more thing I remembered.I was trying to do the matching of my $line
> with the %matchers using the "grep" method. But unfortunately that left me
> without the proper parameters. And somehow I ended up writing my own loops
> that iterate over lines and matchers. 
> Maybe at the current state of code it
> can be refactored into using "grep" ... but with all the power of "grep", I
> was hoping it would help me solve my problem faster, but it turned out to
> be a dead end. In ruby usually, when I pick something to solve a problem
> with - it's either apparent that it wont work from the very start, or it
> just works. Again - it just might be that I am not experienced enough with
> perl to know the intricate ways to make things work.

Your current code reads:

  # tries to match current line with any of the previously stored matchers
  foreach my $key (keys %matchers) {
    if ($line =~ $key) {
      # create an array that includes all matches of /$key/ against $line
      # $#+ is the number of matches in the last regexp
      # @- is an array containing subgroup beginnings (used as $-[$m])
      # @+ is an array containing subgroup ending (used as $+[$m])
      # substr gets $line, offset and length (calculated by end minus 
      my @subgroups;
      foreach my $m ( 1 .. $#+ ) {
        push @subgroups, substr($line,$-[$m],$+[$m]-$-[$m])

I would write it like that: (untested)

while (my ($key, $cb) = each(%matchers))
	if (my @subgroups = ($line =~ $key))

I don't see the point of using grep, map and friends for this because you're 
not interested in the resultant list - you're interested in the side effect. 
So a for/while loop with an if would be best.


	Shlomi Fish

> -
> evgeny

Shlomi Fish       http://www.shlomifish.org/
Best Introductory Programming Language - http://xrl.us/bjn84

God gave us two eyes and ten fingers so we will type five times as much as we

More information about the Perl mailing list