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

I am searching for a way to handle the following data and am not sure how to start. I have looked at arrays, hashes, hashes of hashes, . . . but I have not come across what I need (or maybe I have and just don't know it). Here is what I have.

IP_Address = domain_1 = seconds = seconds = seconds = domain_2 = seconds = seconds = domain_3 = seconds = seconds = seconds

I hope this data makes sense as I couldn't think of any other way to present it. I am comfortable working with arrays and hashes so it would be great if this could be handled with one or the other, or maybe a combination of both. If not, how could I go about handling this data? Note that the data is being read from a file but I need to manipulate it without creating another file.

Thanks much in advance!

Replies are listed 'Best First'.
Re: Data structure assistance
by tadman (Prior) on Oct 16, 2002 at 13:55 UTC
    If you look at each fragment of your data, you'll probably realise how it's structured naturally. Once you've imported it, then the challenge is to restructure as required to generate the required output.

    The first layer is your IP address. These fit naturally in to a hash. The second layer is domains, which will likewise be a good fit in a hash. The third is a list of times, and since you're probably not referencing them directly, these are array elements. For example:
    my %data; $data{$ip_address}{$domain} = [ $time1, $time2, $time3 ]; push (@{$data{$ip_address}{$domain}}, $time4); # ... foreach my $time (@{$data{$ip_address}{$domain}}) { # ... }
    So what you have is a hash of hash of arrays or HoHoA.
Re: Data structure assistance
by BrowserUk (Patriarch) on Oct 16, 2002 at 15:23 UTC

    On the basis of what you've told us (which as George_Sherston said, isn't enough to make a good judgement), I think tadman's method is good except that it means that you need both the ip and the domain name to access the timings. As many ISP's use a single ip to serve several domain names, retaining both peices of information is necessary, but it would be more convenient to only use the domain name for access to the data structure.

    If, in the context of your application, you need/want to use the datastructure to lookup the ip associated with a given domain name then a structure like this might be better.

    my %data; while ( more data ) { my ($ip,$domain,@times) = readNextChunkOfData(); $data{$domain}{'ip'}= $ip; $data{$domain}{'seconds'} = \@times; # the next line isn't necessary, but could be convenient # IF YOU KNOW YOUR IP/DOMAIN NAME PAIRS ARE UNIQUE. # for doing domain-from-ip lookups. $data{$ip}{'domain'} = $domain; }

    That way you can use these lines to lookup the ip from domain or vice versa

    my $domain = $data{$ip}{'domain'};

    my $ip = $data{$domain}{'ip'};

    and this to access the times individually.

     print @{$data{$domain}{'seconds'}}[3]; # 4th timing.

    Or something like this to access them in sequence.

    ... foreach my $time ( @{$data{$domain}{'seconds'}} ) { print $time; }

    Note: Some of the punctation in this post is redundant, but I was trying for clarity

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Data structure assistance
by Zaxo (Archbishop) on Oct 16, 2002 at 13:58 UTC

    That looks like a hash of arrays:

    my @domains = qw( ); my %time_of_ip; $time_of_ip{ @domains } = []; for (@domains) { push @{$time_of_ip{$_}}, timings($_); } sub timings { my $ip = shift; #whatever }

    After Compline,

Re: Data structure assistance
by George_Sherston (Vicar) on Oct 16, 2002 at 13:57 UTC
    Could you say what you want to do with the data? Also, is each row a piece of data? - I *think* what you have shown is a schema rather than the sample data itself, which is rather to prejudge the answer. In my limited experience getting the data structure right at the outset saves a shed load of effort later on, and I'd love to kick in my /50, but with my low-grade version of SOPW::Psychic I'm not sure what the question is.

    George Sherston
Re: Data structure assistance
by rdfield (Priest) on Oct 16, 2002 at 16:04 UTC
    perldsc is a good place to start research regarding data structures. Also worth a read are perllol and perlref.