On Tue, 26 Oct 2004 16:50:11 -0400, Bob Showalter
<[EMAIL PROTECTED]> wrote:
> rs wrote:
> > Hi,
> > Here's a snippet of some code from the cookbook.
>
> Hmm, time to get a new cookbook :~)
Nope. Just make sure you understand the the OP changed the code
quoted from the cookbook, and that the cookbook's code snippets do not
"use strict" in this example.
>
> > I am trying to understand what $seen{$1} is. ie where did $1 come
> > from, and what is in $seen{$1}, and how is the hash populated?
>
> $1 is a built-in variable that is set by capturing parens in a regular
> expression. It's not being set in the script below, and the script below
> doesn't properly capture the unique characters.
>
yup. I agree bob! however, if the OP had correctly quoted the
book, you'd see it does its job:
%seen = ( );
$string = "an apple a day";
foreach $char (split //, $string) {
$seen{$char}++;
}
print "unique chars are: ", sort(keys %seen), "\n";
Also, a couple of paragraphs later, the Cookbook goes on to show how
to solve the same problem with a while loop and a regular expression:
%seen = ( );
$string = "an apple a day";
while ($string =~ /(.)/g) {
$seen{$1}++;
}
print "unique chars are: ", sort(keys %seen), "\n";
In that example, the parens are grabbing a character and dropping it
into $1. Somehow the OP got the two examples mixed up.
Hey OP, since we've pointed out the mix-up, does this clear up your
question? Or do you still not understand what the two above examples
are saying?
--Errin
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>