Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Creating hash Array

by ashnator (Sexton)
on Oct 20, 2008 at 06:50 UTC ( #718152=perlquestion: print w/replies, xml ) Need Help??

ashnator has asked for the wisdom of the Perl Monks concerning the following question:

Howto create a Hash array from a file format like this :- SNPSTER2_50_3_1_119_588 10 SNPSTER2_50_3_1_121_522 16 SNPSTER2_50_3_1_119_260 27 My code #!/usr/bin/perl -w %href; $fn=<>; open(FH,"$fn") || die("Cannot open:$!"); while(<FH>) { @_=split('\t',$_); $key = @_[0]; $href{$key} = @_[1]; while (($key, $value) = each(%href)) { print $key.", ".$value."\n"; } } I am not getting values

Replies are listed 'Best First'.
Re: Creating hash Array
by johngg (Canon) on Oct 20, 2008 at 10:26 UTC
    You are reading the name of the file you wish to process from STDIN but you have not chomped the newline from the string so your open will fail. BTW, it is now recommended to use lexical filehandles and the three argument form of open.

    use strict; use warnings; my $inputFile = <>; open my $inputFH, q{<}, $inputFile or die qq{open: < $inputFile: $!\n};

    This produces

    $ cat spw718152.dat SNPSTER2_50_3_1_119_588 10 SNPSTER2_50_3_1_121_522 16 SNPSTER2_50_3_1_119_260 27 $ ./spw718152 spw718152.dat Unsuccessful open on filename containing newline at ./spw718152 line 8 +, <> line 1. open: < spw718152.dat : No such file or directory $

    Adding the chomp will allow the file to open. Using map and taking advantage of the default behaviour of split, which is to operate on $_ splitting on whitespace (including newlines) and discarding empty trailing fields, gives us this.

    use strict; use warnings; use Data::Dumper; chomp( my $inputFile = <> ); open my $inputFH, q{<}, $inputFile or die qq{open: < $inputFile: $!\n}; my %dataItems = map { split } <$inputFH>; close $inputFH or die qq{close: < $inputFile: $!\n}; print Data::Dumper->Dumpxs( [ \ %dataItems ], [ q{*dataItems} ] );

    Which produces.

    $ ./spw718152 spw718152.dat %dataItems = ( 'SNPSTER2_50_3_1_121_522' => '16', 'SNPSTER2_50_3_1_119_588' => '10', 'SNPSTER2_50_3_1_119_260' => '27' ); $

    I hope this is of interest.

    Cheers,

    JohnGG

      THANKS A LOT Brother.....
Re: Creating hash Array
by Corion (Pope) on Oct 20, 2008 at 06:53 UTC

    By writing code.

    See How (Not) To Ask A Question. I recommend that you show us what code you've already written and how it doesn't do what you want.

Re: Creating hash Array
by Punitha (Priest) on Oct 20, 2008 at 07:41 UTC

    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

      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.
Re: Creating hash Array
by brsaravan (Scribe) on Oct 20, 2008 at 14:19 UTC
    I don't think you are creating hash array here, instead you are just reading the file content and creating a hash. Moreover use "chomp" whenever you are using file handles.
    use strict; use warnings; my %href; my $fn = <>; open(FH, "$fn") || die "Cannot open file"; while (<FH>) { chomp($_); $href{$1} = $2 if $_ =~ /(\S+)\s+(\S+)/; } while (my ($key, $value) = each(%href)) { print $key. ", ". $value."\n"; }
Re: Creating hash Array
by blazar (Canon) on Oct 21, 2008 at 09:30 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2020-08-06 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (37 votes). Check out past polls.

    Notices?