Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

what is something like this - $seen{$1}
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
---

Report this thread to moderator Post Follow-up to this message
Old Post
Rs
10-27-04 01:55 AM


RE: what is something like this - $seen{$1}
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";

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Showalter
10-27-04 01:55 AM


Re: what is something like this - $seen{$1}
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

Report this thread to moderator Post Follow-up to this message
Old Post
Errin Larsen
10-27-04 01:55 AM


Re: what is something like this - $seen{$1}
>
>   %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

Report this thread to moderator Post Follow-up to this message
Old Post
Errin Larsen
10-27-04 01:55 AM


Re: what is something like this - $seen{$1}
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/

Report this thread to moderator Post Follow-up to this message
Old Post
Chasecreek Systemhouse
10-27-04 01:55 AM


Re: what is something like this - $seen{$1}
Simply put, the dot (.) matches everything regardless of modifier switch.

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

Report this thread to moderator Post Follow-up to this message
Old Post
Chasecreek Systemhouse
10-27-04 01:55 AM


Re: what is something like this - $seen{$1}
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Dan Jones
10-27-04 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:49 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.