H
hymie!
Greetings. I don't have that special knack for properly forming a
Google search that will give me the answer I seek, so I apologize if
I'm asking an old question.
I'm taking over a project from a co-worker.
We are processing a file that has information in it:
customer vendor transType productCode appNumber resultCode
We have to prepare 2 reports from this data.
Without too much detail, the first report is sorted by Customer, then by
TransactionType, then by ProductCode, and then by resultCode, with a count
of the number of lines that match each configuration.
The second report is similar, but is sorted by Customer, then by Vendor,
then by TransType, ProdCode, and resultCode.
Co-worker wrote the script with (I don't know the correct term) a multi-
level hash:
unless( exists $list{ $customer } )
{
$list{ $customer } = () ;
}
unless( exists $list{ $customer }{ $type } )
{
$list{ $customer }{ $type } = () ;
}
and so on, down to
unless( exists $list{$customer}{$type}{$productCode}{$appNo}{$vendor} )
{
$list{ $customer }{ $type }{ $productCode }{ $appNo }{ $vendor } =
{ 130 => 0,
150 => 0,
385 => 0 } ;
}
if( exists $list{$customer}{$type}{$productCode}{$appNo}
{$vendor}{$returnCode} )
{
$list{$customer}{$type}{$productCode}{$appNo}{$vendor}{$returnCode} = 1
}
Then he reads the data thusly:
foreach $customer ( keys(%list) )
{
print "Customer: $customer\n\n" ;
foreach $type ( keys(%{$list{ $customer }}) )
{
printf "\n Type: %s", $type ;
foreach $productCode ( keys(%{$list{ $customer }{ $type }}) )
{
printf "\n Product: %s\n", $productCode ;
foreach $appNo (keys(%{$list{ $customer }{ $type }{ $productCode }}))
{
foreach $vendor (keys(%{$list{$customer}{$type}
{$productCode}{$appNo}}))
{
if( $list{ $customer }{ $type }{ $productCode }
{ $appNo }{ $vendor }{385})
{
$no385++ ;
}
if( $list{ $customer }{ $type }{ $productCode }
{ $appNo }{ $vendor }{150})
{
$no150++ ;
}
}
}
}
}
}
This generates the frst report that is sorted by Customer and Type.
He wrote a second, almost identical script to re-parse all of the original
data into a new hash with the variables in customer-vendor-type order.
What I want to know is if there is
(*) an easier way to re-arrange the first hash (visualize taking a column
in a spreadsheet and moving it over, then resorting with the new
column order)?
(*) a better/easier way to start from scratch?
Thanks.
hymie! http://www.smart.net/~hymowitz (e-mail address removed)
===============================================================================
Google search that will give me the answer I seek, so I apologize if
I'm asking an old question.
I'm taking over a project from a co-worker.
We are processing a file that has information in it:
customer vendor transType productCode appNumber resultCode
We have to prepare 2 reports from this data.
Without too much detail, the first report is sorted by Customer, then by
TransactionType, then by ProductCode, and then by resultCode, with a count
of the number of lines that match each configuration.
The second report is similar, but is sorted by Customer, then by Vendor,
then by TransType, ProdCode, and resultCode.
Co-worker wrote the script with (I don't know the correct term) a multi-
level hash:
unless( exists $list{ $customer } )
{
$list{ $customer } = () ;
}
unless( exists $list{ $customer }{ $type } )
{
$list{ $customer }{ $type } = () ;
}
and so on, down to
unless( exists $list{$customer}{$type}{$productCode}{$appNo}{$vendor} )
{
$list{ $customer }{ $type }{ $productCode }{ $appNo }{ $vendor } =
{ 130 => 0,
150 => 0,
385 => 0 } ;
}
if( exists $list{$customer}{$type}{$productCode}{$appNo}
{$vendor}{$returnCode} )
{
$list{$customer}{$type}{$productCode}{$appNo}{$vendor}{$returnCode} = 1
}
Then he reads the data thusly:
foreach $customer ( keys(%list) )
{
print "Customer: $customer\n\n" ;
foreach $type ( keys(%{$list{ $customer }}) )
{
printf "\n Type: %s", $type ;
foreach $productCode ( keys(%{$list{ $customer }{ $type }}) )
{
printf "\n Product: %s\n", $productCode ;
foreach $appNo (keys(%{$list{ $customer }{ $type }{ $productCode }}))
{
foreach $vendor (keys(%{$list{$customer}{$type}
{$productCode}{$appNo}}))
{
if( $list{ $customer }{ $type }{ $productCode }
{ $appNo }{ $vendor }{385})
{
$no385++ ;
}
if( $list{ $customer }{ $type }{ $productCode }
{ $appNo }{ $vendor }{150})
{
$no150++ ;
}
}
}
}
}
}
This generates the frst report that is sorted by Customer and Type.
He wrote a second, almost identical script to re-parse all of the original
data into a new hash with the variables in customer-vendor-type order.
What I want to know is if there is
(*) an easier way to re-arrange the first hash (visualize taking a column
in a spreadsheet and moving it over, then resorting with the new
column order)?
(*) a better/easier way to start from scratch?
Thanks.
hymie! http://www.smart.net/~hymowitz (e-mail address removed)
===============================================================================