Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashes within a hash? 1

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
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:
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?
 
Going along with your existing code (although I shortened up the first part):

Code:
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}) {
      $hash{$src}{$flag}++;
   }
   else {
      [b]$hash{$src}{PTYPE}=$ptype;[/b]
      for (1..5) {
         $hash{$src}{$_}=0;
      }
   }
}

my $header;
[b]my $ptype;[/b]
my $line1;
my $line2;
my $line3;
my $line4;
my $line5;
my $foo;


foreach my $key (sort keys %hash)
  {
     $header.="$key\t";
     [b]$ptype.=$hash{$key}{PTYPE}."\t";[/b]
     $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\t$header" . "\n";
     [b]print "P-TYPE\t\t$ptype" . "\n";[/b]
     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;

produces:

Code:
SOURCE		AAA	BBB	CCC	EEE	
P-TYPE		OSL	OAD	GGG	HIT	
Blank FNAME:	1	0	0	0	
Blank MNAME:	1	1	0	0	
Blank LNAME:	0	0	2	0	
Blank PHONE:	2	1	0	0	
Blank ZIP:	0	0	0	1

but the output is different than your script as fas as the numbers go?

- Kevin, perl coder unexceptional!
 
I see why the values are different. In my code above, change these lines:

Code:
   else {
      $hash{$src}{PTYPE}=$ptype;
      for (1..5) {
         $hash{$src}{$_}=0;
      }
   }

to:

Code:
   else {
      $hash{$src}{PTYPE}=$ptype;
      for (1..5) {
        my $n = ($_ == $flag) ? 1 : 0;
        $hash{$src}{$_}=$n;
      }
   }



- Kevin, perl coder unexceptional!
 
One more thing...can you explain this bit?
Code:
   else {
      $hash{$src}{PTYPE}=$ptype;
      for (1..5) {
        my $n = ($_ == $flag) ? 1 : 0;
        $hash{$src}{$_}=$n;
      }
   }
 
that does what you did here:

Code:
   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}++;
      }
   }
}

but also adds the new ptype data in he hash.

Code:
   else {
      $hash{$src}{PTYPE}=$ptype;[b][COLOR=green]#add the ptype data into the $src hash[/color][/b]

      for (1..5) {[b][COLOR=green]#loop through the values 1 thru 5[/color][/b] 

        my $n = ($_ == $flag) ? 1 : 0;[b][COLOR=green]#Using the ternary operator, check if $flag and $_ are equal. If they are $n = 1, else $n = 0.[/color][/b]

        $hash{$src}{$_}=$n;[b][COLOR=green]#plug the value of $n into each corresponding value of $_ (1..5)[/color][/b]
      }
   }

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top