in reply to Messing with hashes

Assuming I understand your question correctly:
  1. Each file contains only 1 line
  2. format of the line is: key_a|val_a|key_b|val_b
  3. none of the keys or values contain a |
You could use something like:
open(STATS, "/path/to/file") or die "Couldn't open file: $!"; my $line = scalar <STATS>; chomp $line; my %file = split /\|/, $line; close STATS;
Notes: Update: Changed code to deal with trailing newline. Thanks for pointing it out ihb.

Replies are listed 'Best First'.
Re: Messing with hashes
by FireBird34 (Pilgrim) on Feb 07, 2003 at 05:16 UTC
    Thanks for the help -- I got it working.
    open(STATS, "/path/to/file") or die "Couldn't open file: $!"; my %file = split /\|/, scalar <STATS>; close STATS;
    This turned out to be what I needed. As for regex... that scares me still ;) I have to do some more reading up on how that works. Thanks again =)
Re: Re: Messing with hashes
by ihb (Deacon) on Feb 07, 2003 at 15:13 UTC

    Beware of the newline.

    If there is a trailing newline, and there should be according to perlfaq5/"How do I count the number of lines in a file?", you'll get an extra key "\n" with value undef. The obvious way to handle this is to first store the line in a variable, chomp() it, then split() it. A split on /[|\n]/ would work too, but I'd prefer chomp()ing.

    ihb
Re: Re: Messing with hashes
by jdporter (Paladin) on Feb 07, 2003 at 16:28 UTC
    I always get the willies when I see someone reading from a file into an explicit $line variable. (Also, that scalar is superfluous, of course.)

    Here's another way, using map:

    my %file = map { chomp; split /\|/ } <STATS>;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.