in reply to Re: Re: pattern matching
in thread pattern matching
You can reduce the amount of work you're doing to pull out bracketed words by doingmy @story = <FILE>;
This replaces each bracketed work with whatever Replace() returns for that word. The /eg modifiers on the regular expression say to replace whatever is found with the results of executing (/e) the expression on the right-hand side, and to repeat this match "globally" (/g) across the target string. And, because $line is "aliased" to each row in @story, you don't need to make a second pass through @story.foreach my $line ( @story ) { $line =~ s/\[([A-Za-z-]+)\]/Replace($1)/eg; }
Then, to print the story
Now, you've localized all of the interesting work toprint @story;
which I'll leave as an exercise.sub Replace { my $word = shift; ... }
|
|---|