Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: novice with hash/data file question

by chromatic (Archbishop)
on Sep 02, 2008 at 08:25 UTC ( [id://708416]=note: print w/replies, xml ) Need Help??


in reply to noob with hash/data file question

foreach $i ($data[0], $data[1]){

What do you expect this to do? There's no @data in your program; what does it contain?

'$short' => '$place',

The key of this pair is the literal string $short and not the contents of the variable $short. Likewise the value of this pair is the literal string $place and not the contents of the variable $place. When you use a scalar variable in a double-quoted string, Perl interpolates the value of that variable in the string (that is, Perl looks up the value and inserts it into the string). There's no interpolation in single-quoted strings.

%stuff = ( ... )

This will overwrite the hash at each iteration of the loop. Similarly, closing your (unused) filehandle within the body of the loop will close it multiple times.

I suspect you want a program more like:

use strict; use warnings; open my $fh, 'data.txt' or die "Can't read data.txt: $!\n"; my %stuff; while (<$fh>) { chomp; my ($short, $place) = split /\|/, $_; $stuff{$short} = $place; } close $fh;

... but that's a rough guess. (Because you're a novice, let me point out gently that naming a filehandle OUT when you open it for reading will confuse people.)

Replies are listed 'Best First'.
Re^2: novice with hash/data file question
by bhall (Initiate) on Sep 02, 2008 at 08:41 UTC
    Firstly, you're brilliant....

    And the first line was part of something i copied from a different script i made a long time ago

    (apparently a year lull in practice makes what little knowledge I already had go away).

    I had tried to do this a lot of different ways and finally resorted to savaging form previous scripts in hopes of figuring it out, if only by mistake.

    Now that i see the answer written down, it makes sense.

    But yes, you were absolutely correct. I've been googling everything I could find on hashes in perl for hours. Maybe my key words correct, but I couldn't find anything on accessing data from a file through a hash. Thanks a lot.
      Use this code:
      open(FILE, '<', 'data.txt'); my %result = map {split /\|/; ($_[0] => $_[1])} <FILE>; close(FILE);
      It will fulfill all your needs :)
        As well as the issues with the success of the open and with chomp already pointed out by jwkrahn, you should note that the ($_[0] => $_[1]) is superfluous and could be omitted to get the same result.

        my %result = map {split /\|/} <FILE>;

        Using a lexical filehandle inside the scope of a do would save an explicit close.

        use strict; use warnings; use Data::Dumper; my $inFile = q{spw708412.dat}; my %stuff = do { open my $inFH, q{<}, $inFile or die qq{open: < $inFile: $!\n}; map { chomp; split m{\|} } <$inFH>; }; print Data::Dumper->Dumpxs( [ \ %stuff ], [ q{*stuff} ] );

        Running it.

        [johngg@ovs276 perl]$ cat spw708412.dat abc|123 def|456 [johngg@ovs276 perl]$ ./spw708412 %stuff = ( 'def' => '456', 'abc' => '123' ); [johngg@ovs276 perl]$

        I hope this is of interest.

        Cheers,

        JohnGG

        Except that it doesn't verify that the file opened so the filehandle could be invalid and it doesn't chomp so there could be trailing newlines.

      What you need is to firstly read a textbook about perl carefully...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2025-11-11 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.