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

hello,
I want to ask a theoretic question.

my data is(from a radius file)- one record shown:
beurs3-ISDN Password = "Beurs6.65ISDN" User-Service = Framed-User, Framed-Protocol = MPP, Framed-Address = 212.233.6.65, Framed-Netmask = 255.255.255.224, Ascend-Route-IP = Route-IP-Yes, Ascend-Metric = 3
This is read from a flat file. I can parse it but i want to do it nice I thought of putting the username in a array and put the rest in a hash(I think this is a array of hashes).
But I need a feature so you can look-up on username(no prob in the array) and then fetch the rest of the hash. But I also need to give the possibility to look-up on ip. Is this possibly? Can someone guide me or give his views on the best method?

I'm still hacking on it, I will post later what I've found.

--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: hashes & looking up
by cianoz (Friar) on Nov 08, 2000 at 16:51 UTC
    you cannot place the username AND an hash reference in a array. what you need is a hash of hash so you can do
    $usernames{username} = { key1 => value1, ...};
    lookup by username is trivial (my %otherkeys = %{$usernames{that_username}})
    to lookup by ip you need a foreach loop:
    foreach my $key (keys %usernames) { if($usernames{$key}->{Framed-Address} eq $ip_i_look_for) { print "ip $ip_i_look_for owned by user $key \n"; last; } }
Re: hashes & looking up
by Fastolfe (Vicar) on Nov 08, 2000 at 20:41 UTC
    Just to add on to other posts here, if you already have a hash built, keyed on username (such that $hash{$username}->{'Framed-Address'} gets you their IP), you can build a second hash keyed off of IP to simply point to the same data:
    # First build us a 'user' key so that our %iphash # can get the username. foreach (keys %hash) { $hash{$_}->{user} = $_; } @iphash{map { $hash{$_}->{'Framed-Address'} }, keys %hash } = values % +hash;
    Modifications to data under %hash will affect data under %iphash as well, since we're pointing to the same reference.
Re: hashes & looking up
by ChOas (Curate) on Nov 08, 2000 at 17:17 UTC
    I would have to go with Cianoz there
    BUT if you are going to do a lot of look-ups
    By IP, I would consider building a hash of
    the IP addresses too, that would safe a LOT of
    foreach loops

    It's a trade-off you would have to make between
    The speed loss of buiding up your 2nd hash, or spending
    more time later to look up by IP address

    GrtZ!,
      ChOas
      not necessarily if
      $username = $IP{n};

      Have a nice day
      All decision is left to your taste
        *grin*, missed that one...
        Imagine this for a beauty:
        my ($User,$IP,%Info)=ReadItem(@Item); #or something ;))) $LookUp{$User}=$Lookup{$IP}=\%Info;
        $IP and $User would have to be included in %Info ofcourse ;))

        Nice....