Code Comments
Programming Forum and web based access to our favorite programming groups.Hi,
Here's a snippet of some code from the cookbook.
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?
thanks.
Radhika
========================
#!/usr/bin/perl
#use strict;
#use diagnostics;
my %seen = ();
my $string ="an apple a day";
foreach $byte (split //,$string) {
$seen{$1}++;
}
print "unique chars are: ", sort(keys %seen), "\n";
=========================
--
It's all a matter of perspective. You can choose your view by choosing
where to stand.
Larry Wall
---
Post Follow-up to this messagers wrote:
> Hi,
> Here's a snippet of some code from the cookbook.
Hmm, time to get a new cookbook :~)
> 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.
> thanks.
> Radhika
> ========================
> #!/usr/bin/perl
>
> #use strict;
> #use diagnostics;
>
> my %seen = ();
> my $string ="an apple a day";
> foreach $byte (split //,$string) {
> $seen{$1}++;
This should presumably be
$seen{$byte}++;
> }
> print "unique chars are: ", sort(keys %seen), "\n";
Post Follow-up to this messageOn Tue, 26 Oct 2004 16:50:11 -0400, Bob Showalter
<bob_showalter@taylorwhite.com> wrote:
> rs wrote:
>
> 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.
>
>
> $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
Post Follow-up to this message>
> %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?
I realized I should have added a bit more reference information to the
post. For those of you new to the list, "OP" stands for "Original
Poster". In this context, I'm talking about and to Radhika.
For those of you with or without a copy of the "Cookbook" we're
talking about, I'm referring specifically to the "Perl Cookbook, 2nd
Edition" book written by Tom Christiansen, Nathan Torkington. The
code snippets above come from Chapter 1, section 1.6, titled "Recipe
1.6 Processing a String One Character at a Time". I hope that helps
add some context.
-Errin
Post Follow-up to this messageInteresting.
Why doesn't this skip already seen letters, I used the
case-insensitive modifier...
%seen = ( );
$string = "AaBbCcDdEeFf";
while ($string =~ /(.)/gi) {
$seen{$1}++;
}
print "\n\nunique chars are: ", sort(keys %seen), "\n";
'A' and 'a' are the same, or is the logic only char() oriented?
I guess I'm forced to use lc();
--
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/
Post Follow-up to this messageSimply put, the dot (.) matches everything regardless of modifier switch. -- WC -Sx- Jones http://youve-reached-the.endoftheinternet.org/
Post Follow-up to this messageOn Tue, 2004-10-26 at 22:00, ChasecrSystemhouse wrote: > Interesting. > > Why doesn't this skip already seen letters, I used the > case-insensitive modifier... > > %seen = ( ); > $string = "AaBbCcDdEeFf"; > while ($string =~ /(.)/gi) { > $seen{$1}++; > } > print "\n\nunique chars are: ", sort(keys %seen), "\n"; > > 'A' and 'a' are the same, or is the logic only char() oriented? > > I guess I'm forced to use lc(); The 'i' modifier only affects the matching of the RE. If you had a letter in your RE, it would match either case of that letter. It doesn't change the case of the letter, just affects which ones match. Since the RE has no letters, the 'i' modifier doesn't do anything here.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.