[Israel.pm] Another newbie question - Hashes

Yuval Yaari yuval at windax.com
Thu Feb 5 05:11:17 PST 2004


I don't see anything wrong with local $/.
BTW, local automagically sets the variable to undef...

As for your prior e-mail:
My first answer WAS newbie material.
My second wasn't.
My first answer is much closer to yours.

You are totally right about memory usage. If you're stupid enough to 
keep tons of information in a flatfile and not in a DB, that's no my 
fault anyway :)

While is great for memory, pretty straightforward, but you don't want 
~10 lines handling such simple thing. 3-4 lines are usually great.


  --Yuval




Gaal Yahas wrote:

>On Thu, Feb 05, 2004 at 11:40:24AM +0000, Mikhael Goikhman wrote:
>  
>
>>On 05 Feb 2004 13:16:11 +0200, Gaal Yahas wrote:
>>    
>>
>>>If you're going for code that's easy for a beginner to grasp, this is
>>>much clearer:
>>>
>>>     while (<FILE>) {
>>>         my($key, $value) = split /\s+/, $_, 2;
>>>         $hash{$key} = $value;
>>>     }
>>>      
>>>
>>Gaal, Yuval Yaari already mentioned a bug in this code.
>>I noticed this bug too when you posted, but didn't want to interfere. :)
>>
>><> returns a line (or lines in list context) with trailing "\n" in them.
>>So, $value ends up with "\n" in it.
>>
>>To fix this, one solution is to add chomp. Another solution is to remove
>>the third argument from split (but you can't remove it in your original
>>code that converts array to hash, you should use chomp there):
>>
>>  my ($key, $value) = split /\s+/, $_;
>>
>>Some sane file format checks should be added too to avoid other bugs.
>>    
>>
>
>I stand bug corrected!
>
>And yet my points remain:
>- $/ and a temporary var are better not used here, for several reasons.
>- while() is more straightforward and more extensible, and certainly
>  clearer for a beginner. The "sane file format checks" were what I was
>  talking about.
>
>  
>




More information about the Perl mailing list