dnickel has asked for the wisdom of the Perl Monks concerning the following question:

I have a text file with three fields
ex. ("username","realname","password")
I want to store this information in a hash, I am going to be
comparing this hash with a another hash (based on username) and deleting
hash entries if there is a match.
What is the best way to do this with three fields?
Thanks

Replies are listed 'Best First'.
Re: Storing Text file in hash
by theguvnor (Chaplain) on Mar 11, 2002 at 20:10 UTC

    Here's a quick snippet that should suggest the type of structure that could work for the task:

    my %hash; while (my $line=<IN>) { # I assume an open IN filehandle chomp $line; ($username, $realname, $pwd) = split /\t/, $line; # I assume tab del +imiter $hash{$username} = [$realname, $pwd]; }

    Then you can access someuser's realname or password like:

    $realname = $hash{'someuser'}->[0]; $pwd = $hash{'someuser'}->[1];

    My recommendation would be to look at the references manpage for the background on how to build and work with simple data structures.

    Good luck.

    ..Guv

      Thanks.. that should do the trick
Re: Storing Text file in hash
by patgas (Friar) on Mar 11, 2002 at 20:02 UTC
    If the username is always different (which it probably would be), then something like this should get you started:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %userinfo; while ( <DATA> ) { chomp; my ( $username, $realname, $password ) = split /\|/; $userinfo{$username} = { realname => $realname, password => $passw +ord }; } print Dumper \%userinfo; __DATA__ patgas|patrick|foobar doodle|what|now cannot|think|of better|sample|data
    Update: forgot to chomp!

    "We're experiencing some Godzilla-related turbulence..."

Re: Storing Text file in hash
by tune (Curate) on Mar 11, 2002 at 20:04 UTC
    Store an array into the value part of the hash.
    $hash{'username1'}=("realname1", "password1");

    --
    tune