SPUG: Re: Herrewig's newbie questions

El JoPe Magnifico jope-spug at n2h2.com
Thu Sep 23 14:52:50 CDT 1999


On Thu, 23 Sep 1999, Michael Herrewig wrote:
> print "Please enter a few words, one word per line (^D to end)\n";
> chomp(@words = <STDIN>);
> foreach $word (@words) {
>    $count{$word}++;
> }
> foreach $word (keys %count) {
>    print "$word was seen $count{$word} times.\n";
> }
[...]
> 1. at which point is the %count actually created and what values
>    are being injected (I'm assuming numbers)?

%count springs into existence automatically the first time you use it.
In this case, first time through the first for loop at "$count{$word}++;"

After (first time through that loop, anyway) the hash itself is created
(behind the scenes), the hash entry $count{$word} springs into existence,
on the same line, if it didn't already exist.  When first created, the 
value for this hash entry is undef.  

Using the post-increment op (++) on that value makes the undef convert to
(or - dare I use the expression again? - spring into existence as) the
numerical value zero just before performing the increment.

Lot of stuff that happens automagically behind the scenes, all in just
this single line of code!  Which saves you from having to do cumbersome
declarations and initializations yourself.  In perl, 'laziness' is one
of our three virtues (along with 'impatience' and 'hubris').  That said, 
don't let your habits get too lazy if you plan to ever use the 'strict'
pragma someday, or your suffering shall be great indeed.

> 2. does %count only contain keys? the book mentioned nothing of this 

As stated above, each key does have a value, it just starts off as undef
if not initialized otherwise. (I think that's what you're aksing here)

> 3. is there a perl IDE available to set breaks and step through the code
>    and view variable info intermittantly like Qbasic (lame i know)?

See 'perldoc perldebug' for details.  Perl has a built in debugger mode.
A bit kludgy in terms of being a line-oriented interface, but powerful.

There are a few other interfaces that are more user-friendly; see...
http://www.perl.com/reference/query.cgi?debug

> 4. do I read the perldocs with man or perldoc?

perldoc is preferred.  Most packages will also man-ify their documentation,
so you can also use man, but perldoc has some handy additional features.
See 'perldoc perldoc' for details.
-jp


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list