in reply to Hash from a file

use strict; use warnings; my $file = 'file.txt'; open(my $fh, '<', $file) or die("Unable to open input file \"$file\": $!\n"); my %hash; while (<$fh>) { chomp; my ($dtype, $rest) = split(/:/, $_, 2); my ($id, $desc) = split(/\t/, $rest, 2); $hash{$id} = $desc; }

or

while (<$fh>) { my ($id, $desc) = /^[^:]*:([^\t]*)\t(.*)$/; $hash{$id} = $desc; }

Update: Fixed the bug naikonta identified.

Replies are listed 'Best First'.
Re^2: Hash from a file
by naikonta (Curate) on Oct 04, 2007 at 15:45 UTC
    $ perl 642676.pl Can't use global $_ in "my" at 642676.pl line 10, near ", $_" Global symbol "%hash" requires explicit package name at 642676.pl line + 12. Execution of 642676.pl aborted due to compilation errors.

    Reassignment of $_ might be tricky but I don't see any point other than shorter variable name.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!