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.)


In reply to Re: novice with hash/data file question by chromatic
in thread noob with hash/data file question by bhall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.