bigbalbossa
Programmer
Guys...this is going to be a little long, bear with me. The following code with work on the input file data below on a Unix Tru64 system. With the help of this forum, i was able to figure out how to print my hash horizontally, now i need to add the third field in this file ($ptype) to my output...don't have a clue how to do this 
input file:
1|AAA|OSL
1|BBB|OAD
1|AAA|OSL
2|AAA|OSL
2|CCC|GGG
2|BBB|OAD
3|CCC|GGG
3|EEE|HIT
3|CCC|GGG
4|AAA|OSL
4|AAA|OSL
4|BBB|OAD
5|EEE|HIT
Script:
Running this code will produce this output:
SOURCE AAA BBB CCC EEE
Blank FNAME: 2 1 0 0
Blank MNAME: 1 1 1 0
Blank LNAME: 0 0 2 1
Blank PHONE: 2 1 0 0
Blank ZIP: 0 0 0 1
I need to make it look like:
SOURCE AAA BBB CCC EEE
P-TYPE OSL OAD GGG HIT
Blank FNAME: 2 1 0 0
Blank MNAME: 1 1 1 0
Blank LNAME: 0 0 2 1
Blank PHONE: 2 1 0 0
Blank ZIP: 0 0 0 1
Thoughts?
input file:
1|AAA|OSL
1|BBB|OAD
1|AAA|OSL
2|AAA|OSL
2|CCC|GGG
2|BBB|OAD
3|CCC|GGG
3|EEE|HIT
3|CCC|GGG
4|AAA|OSL
4|AAA|OSL
4|BBB|OAD
5|EEE|HIT
Script:
Code:
#!/usr/bin/perl
#use diagnostics;
use strict;
my %hash = ();
open(MYINPUTFILE, "test.dat");
open(OUT, ">test.xls");
while(<MYINPUTFILE>)
{
my $line = $_;
chomp($line);
my($flag,$src, $ptype) = split(/\|/,$line);
if(exists $hash{$src})
{
if ($flag == 1)
{
$hash{$src}{1}++;
}
elsif ($flag == 2)
{
$hash{$src}{2}++;
}
elsif ($flag == 3)
{
$hash{$src}{3}++;
}
elsif ($flag == 4)
{
$hash{$src}{4}++;
}
elsif ($flag == 5)
{
$hash{$src}{5}++;
}
}
else
{
$hash{$src}{1}=0;
$hash{$src}{2}=0;
$hash{$src}{3}=0;
$hash{$src}{4}=0;
$hash{$src}{5}=0;
if ($flag == 1)
{
$hash{$src}{1}++;
}
elsif ($flag == 2)
{
$hash{$src}{2}++;
}
elsif ($flag == 3)
{
$hash{$src}{3}++;
}
elsif ($flag == 4)
{
$hash{$src}{4}++;
}
elsif ($flag == 5)
{
$hash{$src}{5}++;
}
}
}
my $header;
my $line1;
my $line2;
my $line3;
my $line4;
my $line5;
my $foo;
foreach my $key (sort keys %hash)
{
$header.="$key\t";
$line1.=$hash{$key}{1}."\t";
$line2.=$hash{$key}{2}."\t";
$line3.=$hash{$key}{3}."\t";
$line4.=$hash{$key}{4}."\t";
$line5.=$hash{$key}{5}."\t";
}
print "SOURCE\t$header" . "\n";
print "Blank FNAME:\t";
print "$line1" . "\n";
print "Blank MNAME:\t";
print "$line2" . "\n";
print "Blank LNAME:\t";
print "$line3" . "\n";
print "Blank PHONE:\t";
print "$line4" . "\n";
print "Blank ZIP:\t";
print "$line5" . "\n";
close OUT;
Running this code will produce this output:
SOURCE AAA BBB CCC EEE
Blank FNAME: 2 1 0 0
Blank MNAME: 1 1 1 0
Blank LNAME: 0 0 2 1
Blank PHONE: 2 1 0 0
Blank ZIP: 0 0 0 1
I need to make it look like:
SOURCE AAA BBB CCC EEE
P-TYPE OSL OAD GGG HIT
Blank FNAME: 2 1 0 0
Blank MNAME: 1 1 1 0
Blank LNAME: 0 0 2 1
Blank PHONE: 2 1 0 0
Blank ZIP: 0 0 0 1
Thoughts?