CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [BillKSmith] Moving part of Multi-Dimensional Array to an Array: Edit Log

New User

Dec 31, 2012, 11:36 AM

Views: 4537
Re: [BillKSmith] Moving part of Multi-Dimensional Array to an Array

Thanks for the help guys. Sorry if I'm confusing. I tend to work backwards.

I like to start off simple and get it "working" first before making it efficient.

I started my code off by coding it very simple (300 lines).

Then I reviewed and compressed it (100 lines).

I'm still new to perl, so I'm just not good enough to start coding it at the 100 line effiency right off the bat.

7stud's code was useful. push @{ $results{$key} }, $line; should allow me to get rid of my one of my loops having to run through the file multiple times.

I probably could have made the code a little more compressed, but I wanted to calculate the total (_tot) separate from the data to make it easier on my head if I ever have to return to the code.

Here's what I ended up with....

#places the data from the text file into a multi-dimensional array
open FP, "$path/Analysis.txt";
@arr=split /\t/, $r;
$dt_str = sprintf "%s %d", $mon[substr($arr[0],4)], substr($arr[0],0,4);
$nr = "$arr[5]\t$arr[6]\t$arr[7]\t$arr[8]";
push @{ $md[$arr[3]] }, $r;
push @{ $pp_tot{$arr[1]}{$arr[0]} }, $nr;
close FP;

$mon_cnt = scalar @{$md[$arr[3]]};

#calculates the total for each Group
foreach $lf_cd (sort keys %pp_tot)
$nr = "";
foreach $yrmo (reverse sort keys %{$pp_tot{$lf_cd}})
$lv_pp_tot = 0;
$lv_pplss_tot = 0;
$pp_dis_tot = 0;
$pplss_dis_tot = 0;
for ($i=0; $i < scalar @{$pp_tot{$lf_cd}{$yrmo}}; $i++)
($lv_pp, $lv_pplss, $pp_dis, $pplss_dis) = split /\t/, $pp_tot{$lf_cd}{$yrmo}[$i];
$lv_pp_tot = $lv_pp_tot + $lv_pp;
$lv_pplss_tot = $lv_pplss_tot + $lv_pplss;
$pp_dis_tot = $pp_dis_tot + $pp_dis;
$pplss_dis_tot = $pplss_dis_tot + $pplss_dis;
$nr .= sprintf "<b>$lv_pp_tot\t<b>$lv_pplss_tot\t<b>$pp_dis_tot\t<b>$pplss_dis_tot\t<right><b>%.1f%%\t<right><b>%.1f%%\t", 100*($pp_dis_tot/$lv_pp_tot), 100*($pplss_dis_tot/$lv_pplss_tot);
$pp_tot{$lf_cd} = $nr;

#prints all the data

open FH, ">$fn1" || die "Cannot open $fn1";
foreach $key (sort keys %pp_tot)
for ($i = 1; $i < (scalar @md) + 1; $i++)
@grp = @{$md[$i]};
foreach $r (reverse sort(@grp))
($yrmo, $lf_cd, $lf_nm, $cnx_cd, $cnx_nm, $lv_pp, $lv_pplss, $pp_dis, $pplss_dis) = split /\t/, $r;
if ($key eq $lf_cd && $i == $cnx_cd)
$lf_nm_tot = $lf_nm;
if ($j==1)
printf FH "\n<left>$lf_cd\t<left>$lf_nm\t<right>$cnx_cd\t<left>$cnx_nm\t<right>$lv_pp\t<right>$lv_pplss\t<right>$pp_dis\t<right>$pplss_dis\t<right>%.1f%%\t<right>%.1f%%\t", 100*($pp_dis/$lv_pp), 100*($pplss_dis/$lv_pplss);
printf FH "<right>$lv_pp\t<right>$lv_pplss\t<right>$pp_dis\t<right>$pplss_dis\t<right>%.1f%%\t<right>%.1f%%\t", 100*($pp_dis/$lv_pp), 100*($pplss_dis/$lv_pplss);
printf FH "\n\t<left><b>$lf_nm_tot Total\t\t\t%s\n", $pp_tot{$key};
print FH "\n";
close FH;

(This post was edited by lilmark on Dec 31, 2012, 11:50 AM)

Edit Log:
Post edited by lilmark (New User) on Dec 31, 2012, 11:50 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives