On Tue, 1 Jun 2004, Jeff 'japhy' Pinyan wrote:

> On Jun 1, Dennis G. Wicks said:
>
> >Greetings;
>
> Please start a new thread next time, instead of replying to a post and
> creating a new topic.

OK, so I screwed up. Mea culpa!

>
> >      foreach (<FD>) {
> >        chomp;
> >        s/^ *//;
> >        ($rank, $artist) = split(/ /, $_, 2);
>
> Those two lines (the s/// and the split) could be rewritten as:
>
>   ($rank, $artist) = split ' ', $_, 2;
>
> The ' ' argument to split() ignores leading and trailing whitespace.

Thanks for the tip. I develop stepwise. The s/^ *//; came
about because I encountered some records that had leading
spaces, and right now "If it works, don't f*** with it!".
>
> >>>      if ($name{$artist} eq "") {$cnt{$artist} = 0 ; $r{$artist} = 0 }
>
> Simply do:
>
>   if (not exists $name{$artist}) { ... }

Alright! That's what I was looking for! Works great!

>
> The reason you're getting the error is, when the key $artist doesn't exist
> in %name, $name{$artist} returns undef, and using undef in a comparison
> like '==' or 'eq' gives you the warning message you described.
>
> Using the exists() function is more appropriate here.
>
> >        $r{$artist} = $r{$artist} + $rank;
>
>   $r{$artist} += $rank;
>
> >        $cnt{$artist} = $cnt{$artist} + 1;
>
>   ++$cnt{$artist};
 Yeah, I originally had code like you suggest, but I was
getting the bad error messages and I thought I could fool it
somehow.

>
> >        $name{$artist} = $artist;
> >        print qq($rank, $artist), "\n";
> >        print qq($r{$artist}, $cnt{$artist}, $name{$artist}), "\n";
>
> Why don't you include the \n in the qq() strings?  Or, why do you feel the
> need to use qq() here when a double-quoted string is fine?
>
> >        }
>
> You might want to start including 'use strict' in your code; be aware,
> though, that this will require you to declare your variables.  Please read
> variable scoping documentation to avoid headaches and frustration.
I do use ... this is just a snippet and the headers,

        #!/usr/bin/perl
        use diagnostics;
        use warnings;
        use strict;

are way above the code in question.

Thanks for the help and the tips!
Dennis
>
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to