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

Hey all, this is a report that i was working on and it is almost done execpt that i need the order it by the group attribute. Basically this is creating a consolidated report out of two flat files(devtools.data & sysinfo.data). The "sysinfo" file holds information about the system and the "devtools" file hold information about the "attributes" of that system. The little script is attached together with an example of the data files. The code is very self explanitory.( there are some commented out parts that i did'nt clean yet, sorry)

edited: Wed Jun 19 14:52:32 2002 by jeffa - added reamore tag

 

 

#!/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; #print "$sys - $pp\n"; } 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 "$sys - $pp\n"; } #print Dumper (\%HoH); my @array = ("Support Tools", "Source Code Management", "Production Pl +atforms", "Design Tools", "Test Tools", "Database", "Middleware", "Database Tools", + "Found Internal Libraries", "Development Languages", "Development Tools", "Fail Over +Software"); my %sorted_by_attribute; #Loop through all the systems foreach my $system (keys %HoH) { #print "$system\n"; foreach my $attribute (@array) { #foreach my $attribute (keys %{$HoH{$system}}) { #print "\t$attribute\n"; #Get the value of that key, eg '8', my $attribute_value = $HoH{$system}{$attribute +}; #print "\t\t$attribute_value\n"; push @{$sorted_by_attribute{$attribute}{$attri +bute_value}}, $system; } } #print Dumper (\%sorted_by_attribute); #Print the data foreach my $attribute (sort keys %sorted_by_attribute) { my $individual_values = $sorted_by_attribute{$attribut +e}; print $attribute, "\n"; foreach my $value (keys %$individual_values) { my @systems = @{$sorted_by_attribute{$attribut +e}{$value}}; foreach $system (@systems) { print "\t$HoH{$system}{'Group'}\n"; print "\t\t$HoH{$system}{'System Name'}"; print " = > $value\n +"; } } print "\n"; }

This is sysinfo.data

1017939028902!SYS-J8!DSS!rossa 1017939160023!NC-SYS!SALES!dstone 1017939144004!SYS-MR2!DSS!rossa 1017939173029!SYS-MG!DSS!rossa 1017939186030!IAT-SYS!FINANCE!rsanders 1017939213033!SYS-V!DSS!rossa 1017939229038!GTR-SYS!MARKETING!ehugo 1017939261051!SYS-E!DSS!rossa 1017939272053!PA-SYS!FINANCE!rsanders 1017939284055!SOA-SYS!FINANCE!rsanders

This is devtools.data

1017939173029!1!2!3!4!5!6!7!8!9!10!tt-029!12!http://www.perl.com!Fri M +ar 29 10:32:44 2002! 1017939229038!1!2!3!4!5!6!7!8!9!10!tt-038!12!http://www.perl.com!Fri M +ar 29 10:32:44 2002! 1017939028902!1!2!3!4!5!6!7!8!9!10!tt-902!12!http://www..perl.com!Fri +Mar 29 10:32:44 2002! 1017939144004!1!2!3!4!5!6!7!8!9!10!tt-004!12!http://www..perl.com!Fri +Mar 29 10:32:44 2002! 1017939284055!Sun Enterprise 4500, Sun Enterprise 6500, OS: Solaris 2. +6!None!Qualix HA+!Sybase SQL Adaptive Server 11.9.2!JCon nect, ESQLC!C, C++, Java, JavaScript, Korn Shell!None!Ghostscript, App +endPDF, HTLMLDOC, ImageMagick, Java Runtime Environment, JRun Pro, Netscape Enterprise Server, Virtual Frame Buffer, GeoSystem +s GeoLocate, EnterpriseSoft Report Writer, HP Open View, IBM Date, JAF, JavaMail, SDSU, XPM!Sun C++ Workshop, JDK, GNU make, J +ikes!Sun Workshop Teamware!None!MS Powerpoint, MS Word, Netscape Composer, UltraEdit, Emacs, Together J, Visio!http://www..per +l.com/CRPD/!Mon Apr 15 10:48:25 2002! 1017939186030!cerpp!cermidd!cerfos!cerdb!cerdbt!cerdlang!cerdxi!cerdto +ols!cerSTOOLS!cerscm!certt!cerdt!http://www..perl.com/CR PD!Thu Apr 18 14:26:28 2002! 1017939213033!EGpp!EGmidd!EGfos!EGdb!EGdb!EGdl!EGdxi!EGdtools!EGstools +!EGscm!EGtt!EGdest!http://www.perl.com/electronic_gatek eeper!Thu Apr 18 14:29:30 2002! 1017939261051!GTRpp!GTRmidd!GTRfos!GTRbd!GTRdbt!GTRdlang!GTRfxi!GTRdto +ols!GTRstools!GTRscm!GTRtt!GTRdest!http://www.perl.com/ PACR!Thu Apr 18 14:31:06 2002! 1017939272053!Sun OS !none !none !none !none !none !none !none !none ! +none !none !none !http://www.perl.com/CRPD/!Mon Jun 10 13:54:07 2002! 1017939160023!Sun OS !none !none !none !none !none !none !none !none ! +none !none !none !http://www.perl.com/CRPD/!Mon Jun 10 13:54:07 2002!

Currently the result looks like the following (which is really close to the format needed):

Database = > FINANCE IAT-SYS = > cerdb SALES NC-SYS = > none FINANCE PA-SYS = > none ACCOUNTING SYS-MR2 = > 4 ACCOUNTING SYS-J8 = > 4 MARKETING GTR-SYS = > 4 ACCOUNTING SYS-MG = > 4 ACCOUNTING SYS-V = > EGdb FINANCE SOA-SYS = > Sybase SQL Adaptive Server 11.9.2 ACCOUNTING SYS-E = > GTRbd Database Tools = > FINANCE IAT-SYS = > cerdbt ACCOUNTING SYS-E = > GTRdbt SALES NC-SYS = > none FINANCE PA-SYS = > none ACCOUNTING SYS-V = > EGdb ACCOUNTING SYS-MR2 = > 5 ACCOUNTING SYS-J8 = > 5 MARKETING GTR-SYS = > 5 ACCOUNTING SYS-MG = > 5 FINANCE SOA-SYS = > JConnect, ESQLC Design TOols Blah Blah Blah... Blah..

Now if you look at the above output, you see they are sub-divided by departments(groups) and then the "sytems" and ther value.
HERE IS THE QUESTION
This one i am clueless how to do this with how my code is set up? How can i make the output cleaner and by arranging it in a more orderly fashon like so:

Database ACCOUNTING SYS-MR2 = > 4 SYS-J8 = > 4 SYS-MG = > 4 SYS-V = > EGdb SYS-E = > GTRbd FINANCE IAT-SYS = > cerdb PA-SYS = > none SOA-SYS = > Sybase SQL Adaptive Server 11.9.2 MARKETING GTR-SYS = > 4 SALES NC-SYS = > none Database Tools Blah .. Blah .. Development Tools . . .

I would also like you guys to comment on the code and see if there is a more elegant way to go about this. Thank You in advance.

Replies are listed 'Best First'.
Re: Reports using perl
by mephit (Scribe) on Jun 20, 2002 at 05:06 UTC
    Here's something that I think does what you want. I'm sure all this can be done in place of that %sorted_by_attribute hash, but I'd need to look over the code some more to figure it out. Anyway, just replace your "print" loop with the following, then loop through %newhash to get at your data:
    my %newhash; foreach my $attribute (sort keys %sorted_by_attribute) { my $individual_values = $sorted_by_attribute{$attribute}; foreach my $value (keys %$individual_values) { my @systems = @{$sorted_by_attribute{$attribute}{$value}}; foreach $system (@systems) { my $key = $HoH{$system}{'Group'}; my $str = sprintf "%s = > %s\n", $HoH{$system}{'System Name'}, $ +value; push @{ $newhash{$attribute}{$key} }, $str; } } } print Dumper (\%newhash);
    I'm sure the $key and $str variables can be done away with and those expressions put directly into the push statement, but it would be rather ugly. Anyway, I hope this does what you want.

    --

    There are 10 kinds of people -- those that understand binary, and those that don't.