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


Background
Hi, all i had been messing over this script before i got sick last month and did not get a chance to post here. I am a novice with perl. This script is the basic old stuff where you pull information out of a flat file and lay it out in some elegant form. I have done this with DBI/SQL but here I have a different dilemma. In this script, I have to do this using data structures because the its essentially going to be a small flat file and using a database is not efficient here - (or I would have loverd to use SQL on this one :-( ).

PROBLEM
The problem I have been having is essentially with the data structure part of the script. I have played with the following code and will elaborate.

CODE

#!/usr/local/bin/perl use Data::Dumper; # GET DEVTOOLS open (DTOOLS, "devtools.data"); @devtools= <DTOOLS>; close (DTOOLS); # GET SYSINFO open (SYS, "sysinfo.data"); @system= <SYS>; close (SYS); foreach $line (@devtools) { ($sys, $pp, $midd, $fos, $db, $dbt, $dlang, $fil, $dtools, $st +ools, $scm, $tt, $dest, $url, $lastupdate) = split (/!/, $line); # Build HoH for devtools; $HoH{$sys}{'Production Platforms'} = $pp; $HoH{$sys}{'Middleware'} = $midd; $HoH{$sys}{'Fail Over Software'} = $fos; $HoH{$sys}{'Database'} = $db; $HoH{$sys}{'Database Tools'} = $dbt; $HoH{$sys}{'Development Languages'} = $dlang; $HoH{$sys}{'Found Internal Libraries'} = $fil; $HoH{$sys}{'Development Tools'} = $dtools; $HoH{$sys}{'Support Tools'} = $stools; $HoH{$sys}{'Source Code Management'} = $scm; $HoH{$sys}{'Test Tools'} = $tt; $HoH{$sys}{'Design Tools'} = $dest; $HoH{$sys}{'URL'} = $url; $HoH{$sys}{'Last Update'} = $lastupdate; } foreach $line (@system) { ($sys, $sys_name, $grp, $auth) = split (/!/, $line); # Build HoH for devtools; $HoH{$sys}{'System Name'} = $sys_name; $HoH{$sys}{'Group'} = $grp; $HoH{$sys}{'Authorization'} = $auth; } ##print Dumper(\%HoH);

Ok What i did above is open 2 files and set their data up is a Hash of Hash called HOH.
Here is what the data looks like if i do a print Dumper(\%HoH);.

$VAR1 = { '1018884184639' => { 'Group' => 'Marketing', 'System Name' => '1018884184639', 'Authorization' => 'mrossa' }, '1017939213033' => { 'Group' => 'Sales', 'System Name' => '1017939213033', 'Last Update' => 'Thu Apr 18 14:29:30 2 +002', 'Support Tools' => 'EGstools', 'Source Code Management' => 'EGscm', 'Production Platforms' => 'EGpp', 'Design Tools' => 'EGdest', 'Authorization' => 'dsmith', 'Test Tools' => 'EGtt', 'Database' => 'EGdb', 'URL' => 'http://www.perlmonks.com', 'Middleware' => 'EGmidd', 'Database Tools' => 'EGdb', 'Found Internal Libraries' => 'EGfdxi', 'Development Languages' => 'EGdl', 'Development Tools' => 'EGdtools', 'Fail Over Software' => 'EGfos' }, '1017939028902' => { 'Group' => 'Shipping', 'System Name' => '1017939028902', 'Last Update' => 'Fri Mar 29 10:32:44 2 +002', 'Support Tools' => '9', 'Source Code Management' => '10', 'Production Platforms' => '1', 'Design Tools' => '12', 'Authorization' => 'ecartman', 'Test Tools' => '11', 'Database' => '4', 'URL' => 'http://www.osdncom', 'Middleware' => '2', 'Database Tools' => '5', 'Found Internal Libraries' => '7', 'Development Languages' => '6', 'Development Tools' => '8', 'Fail Over Software' => '3' }, };

QUESTION
No. 1 How can i make each reacord (record is the number) here into a single line delimited by something.

No. 2 (this is the one that i have been stuck on)
How can i print this by CLASSIFICATION TYPE. By that i mean that if you look close at the data. It is classified by things like (database, database-tools, test-tools etc.) Now i was trying to print this like so:

Database: MySQL - Marketing....etc Oracle - Sales....etc Sybase - Shipping....etc Database-tools: Tool-x - Accunting....etc Tool-y - SHipping...etc Tool-z - IT...etc Test-tools T-Tool-x - IT...etc T-Tool-y - Accuniting...etc T-Tool-z - Sales...etc

and so on....

You see what i tried to do is classyify each record by a specific type of data.
As always, any input will go a long ways.
( GO ARGENTINA, WIN WIN WIN WORLD CUP!!!!)

Replies are listed 'Best First'.
Re: Data Structures and Sorting
by robobunny (Friar) on Jun 11, 2002 at 18:01 UTC
    your example seems a little bewildering to me. maybe i'm just not reading it right, but i'm not sure what parts of your example refer to what elements of your hash. also, it seems as though you are putting two different kinds of data in the same hash? even if they are similar, you might want to consider splitting the data into two different hashes, so that it is easier to process.
      I may have not clarified this but the data is normalized data, not just a data dump.
      One file contains information relivant to the system. The second file contains information about its owner. A the file which contains the system information has a "primary key" and that is it! This primary key is the identifying number you see in the Data::Dumper example. For you to know what a id means you have to look it up in the "master" file and you get things like Name, Owner, Group etc.

      Now when you say that to you it looks like two different kinds of data. That is false. The data is INTERDEPENDENT.

      Example:

      File 1: ID, Name, Group, Owner File 2: ID, Oracle, Tibco, AS/400, blah blah blah....

      Hope this clarifies it further. Thanks,

Re: Data Structures and Sorting
by DamnDirtyApe (Curate) on Jun 12, 2002 at 05:01 UTC
    In this script, I have to do this using data structures because the its essentially going to be a small flat file and using a database is not efficient here - (or I would have loverd to use SQL on this one :-( ).

    I realize that there's a lot of value in learning how to use some more advanced constructs (AoA, AoH, HoH...), but if you still want to use SQL without the database, check out DBD::CSV. Here you can use the DBI just as if you were connected to a more sophisticated DBMS, but your tables will be simply comma-delimited text files.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: Data Structures and Sorting
by davis (Vicar) on Jun 12, 2002 at 09:19 UTC
    Hi there,
    I had a pop at this, and ended up with a solution that's a bit too much like line noise. Anyway, here you go:
    #!/usr/bin/perl use warnings; use strict; #Your data, I messed with the Group attribute to test my code. my $hoh = { '1018884184639' => { 'Group' => 'Marketing', 'System Name' => '1018884184639', 'Authorization' => 'mrossa' }, '1017939213033' => { 'Group' => 'Marketing', 'System Name' => '1017939213033', 'Last Update' => 'Thu Apr 18 14:29:30 2002', 'Support Tools' => 'EGstools', 'Source Code Management' => 'EGscm', 'Production Platforms' => 'EGpp', 'Design Tools' => 'EGdest', 'Authorization' => 'dsmith', 'Test Tools' => 'EGtt', 'Database' => 'EGdb', 'URL' => 'http://www.perlmonks.com', 'Middleware' => 'EGmidd', 'Database Tools' => 'EGdb', 'Found Internal Libraries' => 'EGfdxi', 'Development Languages' => 'EGdl', 'Development Tools' => 'EGdtools', 'Fail Over Software' => 'EGfos' }, '1017939028902' => { 'Group' => 'Shipping', 'System Name' => '1017939028902', 'Last Update' => 'Fri Mar 29 10:32:44 2002', 'Support Tools' => '9', 'Source Code Management' => '10', 'Production Platforms' => '1', 'Design Tools' => '12', 'Authorization' => 'ecartman', 'Test Tools' => '11', 'Database' => '4', 'URL' => 'http://www.osdncom', 'Middleware' => '2', 'Database Tools' => '5', 'Found Internal Libraries' => '7', 'Development Languages' => '6', 'Development Tools' => '8', 'Fail Over Software' => '3' }, }; #We have a hash of system hashes, and we want to sort/group by the key +s within #the system hashes. #First, find out what attributes you're going to sort/group by my %sorted_by_attribute; #Loop through all the systems foreach my $system (keys %$hoh) { #Loop through all the keys found within each system, eg 'Devel +opment Tools' foreach my $attribute (keys %{$hoh->{$system}}) { #Get the value of that key, eg '8', and push the syste +m name onto #an array which is unique to that attribute, then the +value of that attribute my $attribute_value = $hoh->{$system}{$attribute}; push @{$sorted_by_attribute{$attribute}{$attribute_val +ue}}, $system; #Offhand, $sorted_by_attribute{$attribute}{$attribute_ +value} is an array reference, #hence the deref'ing curlies and @ symbol. (You can't +put a straight array into #a hash) } } #Now %sorted_by_attributes looks like this: #$VAR1 = { # 'Group' => { # 'Marketing' => [ # '1017939213033', # '1018884184639' # ], # 'Shipping' => [ # '1017939028902' # ] # }, # . # . # . # } #Print the data foreach my $attribute (sort keys %sorted_by_attribute) { my $individual_values = $sorted_by_attribute{$attribute}; print $attribute, "\n"; foreach my $value (keys %$individual_values) { my @systems = @{$sorted_by_attribute{$attribute}{$valu +e}}; print $value, " := "; print join ", ", @systems; print "\n"; } print "\n"; }
    Blech.
    Hopefully the comments are enough to allow you to understand it
    cheers

    davis
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
    Update: Fixed missing octothorp for comment. D'oh.