Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

WWWXXXAPC, docmd.close 2585, WWWXXXDOCO, nu vot, dhcpd lease file "binding state", WWWXXXDOCO, how to setup procmail to process html2text, how to setup procmail html2text, WWWXXXAPC., XXXCNZZZ

Links

XODOX
Impressum

#1: Learning Perl ex8-5

Posted on 2011-09-27 04:57:49 by Chris Stinemetz

I am having some difficulty completing this exercise from llama book.

Maybe someone can explain memory variables to me when working with
regular expressions.

The exercise:

Modify the program from the previous exercise so that immediately
following the word ending in "a" it will also capture up to five
characters (if three are that many characters, of course)
in a separate memory variable. Update the code to display both memory
variables. For example, if the input string says "I saw Wilma
yesterday", the up-to-five characters are yest. If the input is I,
Wilma!, the extra memory should have just one character. Does your
pattern still match just plain wilma?

Program from previous exercise:



#!/usr/bin/perl

use warnings;
use strict;

# This next line of code is used when you get to Chapter 9.
my $what = 'fred|barney';

while (<STDIN>) {
chomp;
# If you want to try matching strings which may contain
# newlines, here's the trick to use: Uncomment this next
# line, then use a pound sign ("#") wherever you mean to
# have a newline within your data string.
# s/#/\n/g;

if (/(?<word>\b\w*a\b)/) {
print "Matched: |$`<$&>$'|\n";
print "'word' contains '$+{word}'\n"; # The new output line

# If you need these for testing patterns with
# memories, uncomment them as well
# print " And memory one got <$1>\n";
# print " And memory two got <$2>\n";
} else {
print "No match: |$_|\n";
}
}


excercise hints:

m!
(\b\w*a\b)
(.{0,5})
!xs

Thank you,

Chris

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#2: Re: Learning Perl ex8-5

Posted on 2011-09-27 07:30:34 by Jim Gibson

At 9:57 PM -0500 9/26/11, Chris Stinemetz wrote:
>I am having some difficulty completing this exercise from llama book.
>
>Maybe someone can explain memory variables to me when working with
>regular expressions.
>
>The exercise:
>
>Modify the program from the previous exercise so that immediately
>following the word ending in "a" it will also capture up to five
>characters (if three are that many characters, of course)
>in a separate memory variable. Update the code to display both memory
>variables. For example, if the input string says "I saw Wilma
>yesterday", the up-to-five characters are yest. If the input is I,
>Wilma!, the extra memory should have just one character. Does your
>pattern still match just plain wilma?
>

"Memory variables" are also called "capture buffers" and "numbered
match variables". See 'perldoc perlre' and search for 'Capture
buffers':

"The bracketing construct "( ... )" creates capture buffers. To refer to
the current contents of a buffer later on, within the same pattern, use
\1 for the first, \2 for the second, and so on. Outside the match use
"$" instead of "\"."

>excercise hints:
>
>m!
> (\b\w*a\b)
> (.{0,5})
>!xs

That regular expression contains 2 capturing parentheses pairs.
Therefore, after a successful match, the matched substrings will be
stored in $1 and $2.

--
Jim Gibson
Jim@Gibson.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message