FAQ
I am trying to traverse a file and match the key to a key in a second
file...

How do I put the file into the Hash???

%hash = "c:/file.txt";

I cant seem to find the magic here..

TIA

Search Discussions

  • Kipp, James at Jun 4, 2002 at 6:14 pm
    how is the file laid out?

    -----Original Message-----
    From: Ned Cunningham
    Sent: Tuesday, June 04, 2002 2:02 PM
    To: 'beginners@perl.org'
    Subject: Read a file into a Hash


    I am trying to traverse a file and match the key to a key in a second
    file...

    How do I put the file into the Hash???

    %hash = "c:/file.txt";
  • Ned Cunningham at Jun 4, 2002 at 6:16 pm
    It's a pipe delimited txt file.

    -----Original Message-----
    From: Kipp, James
    Sent: Tuesday, June 04, 2002 2:15 PM
    To: 'beginners@perl.org'
    Subject: RE: Read a file into a Hash

    how is the file laid out?

    -----Original Message-----
    From: Ned Cunningham
    Sent: Tuesday, June 04, 2002 2:02 PM
    To: 'beginners@perl.org'
    Subject: Read a file into a Hash


    I am trying to traverse a file and match the key to a key in
    a second
    file...

    How do I put the file into the Hash???

    %hash = "c:/file.txt";



    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Daniel Kelley at Jun 4, 2002 at 6:21 pm
    hi-

    just did some work w/ a pipe-delimited file:

    assuming that you've already cleaned the data to make sure that there are
    no random pipes in there, something like the following should work:

    open(NEW, "$cwd/txt/new.txt") or die "couldn't open new.txt";

    while ($line = <NEW>) {
    chomp($line);
    ($usrid, $alias, $passwd, $fname, $mname, $lname)
    = split /\|/, $line, 6;

    next if (exists $hoh{$usrid});

    $hoh{$usrid} = {
    alias => $alias,
    passwd => $passwd,
    fname => $fname,
    lname => $lname,
    mname => $mname,
    };
    }

    close NEW;
    It's a pipe delimited txt file.

    -----Original Message-----
    From: Kipp, James
    Sent: Tuesday, June 04, 2002 2:15 PM
    To: 'beginners@perl.org'
    Subject: RE: Read a file into a Hash

    how is the file laid out?

    -----Original Message-----
    From: Ned Cunningham
    Sent: Tuesday, June 04, 2002 2:02 PM
    To: 'beginners@perl.org'
    Subject: Read a file into a Hash


    I am trying to traverse a file and match the key to a key in
    a second
    file...

    How do I put the file into the Hash???

    %hash = "c:/file.txt";



    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Janek Schleicher at Jun 4, 2002 at 6:29 pm

    Ned Cunningham wrote at Tue, 04 Jun 2002 20:16:46 +0200:

    It's a pipe delimited txt file.
    Examples always help us.
    I assume your file looks like

    key1|value1
    key2|value2
    ...
    keyn|valuen

    Then you should try:
    open FILE, "<$filename" or die "Can't open $filename: $!";
    my %hash = map {chomp; split /\|/} (<FILE>);
    close FILE;

    Magic, isn't it ?!

    Best Wishes,
    Janek

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 4, '02 at 6:02p
activeJun 4, '02 at 6:29p
posts5
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase