For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > what is something like this - $seen{$1}









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author what is something like this - $seen{$1}
Rs

2004-10-26, 8:55 pm

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
---
Bob Showalter

2004-10-26, 8:55 pm

rs 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";

Errin Larsen

2004-10-26, 8:55 pm

On 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
Errin Larsen

2004-10-26, 8:55 pm

>
> %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
Chasecreek Systemhouse

2004-10-26, 8:55 pm

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();

--
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/
Chasecreek Systemhouse

2004-10-26, 8:55 pm

Simply put, the dot (.) matches everything regardless of modifier switch.

--
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/
Dan Jones

2004-10-26, 8:55 pm

On Tue, 2004-10-26 at 22:00, Chasecr Systemhouse 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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com