Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Creating hash Array

by Punitha (Priest)
on Oct 20, 2008 at 07:41 UTC ( #718158=note: print w/ replies, xml ) Need Help??


in reply to Creating hash Array

Hi ashnator

'@_' is the special variable used for subroutine arguments, instead you should use the array variable like

use strict; my %href; my $fn=<>; open(FH,"$fn") || die("Cannot open:$!"); while(<FH>) { my @array =split('\t',$_); ###@array is the array variable my $key = $array[0]; $href{$key} = $array[1]; } while (my ($key, $value) = each(%href)) { print $key.", ".$value."\n"; }

Punitha


Comment on Re: Creating hash Array
Select or Download Code
Re^2: Creating hash Array
by blazar (Canon) on Oct 21, 2008 at 09:40 UTC

    I personally believe that while @_ should certainly not be used as a general purpose variable, @array is as a horrible a name as one can be. It's like having a dog and calling it "dog!!" FWIW, a single letter variable would be better, especially here, since it's used only in two lines - except that I would use two scalars instead. Even better: since the whole file is slurped anyway, how 'bout the following?

    my %data = map { chomp; split /\t/, $_, 2 } <FH>;

    (Of course, it will fail if there are not enough entries for some record. In which case it would require some sanitizing which is left as an exercise to the OP.)

    --
    If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://718158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-02 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (33 votes), past polls