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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash merge? 3

Status
Not open for further replies.

klkot

Programmer
Joined
Jun 6, 2007
Messages
18
Location
US
The following file I created works to create the dataset I want for each file individually. I would have loaded each file and the two separate datasets would come out like this:
Medication 1 Dataset
Patientid Adhash{patientid}
1223 .95
1224 .56
1225 .78
1226 .99
1227 .67

Medication 2 Dataset
Patientid Adhash{patientid}
1226 .85
1227 .66
1487 .45
1489 .99
1500 .32
Now I want to use my same code to combine two files by Patientid key again WHILE CREATING DISTINCT HASHES(VARIABLES) FOR EACH FILE'S ADHASH{PATIENTID} so that the output will look like:

Patientid ADHash{patientid}Med1 ADHash{patientid}Med2
1223 .95
1224 .56
1225 .78
1226 .99 .85
1227 .67 .66
1487 .45
1489 .99
1500 .32



Relevant portion of code for individual file:


open(FH ,"$file")||die "Couldn't open infile";
...
...
...
...etc,

my %dmhash = ();
my %dohash = ();
my %adhash = ();
for ($i=1; $i<@patientid; $i++){
$dmhash{$patientid[$i]} += $intdaymiss[$i];
$dohash{$patientid[$i]} += $daysobs[$i];}

#Creating an adherence "hash" (adhash) that is calculated from the daysmissed hash (dmhash) and daysobserved hash (dohash)
for $id (keys %dmhash){
if(($dohash{$id})==0) {
$adhash{$id}=nil;
}
else{$adhash{$id} = 1-(($dmhash{$id})/($dohash{$id}));
}
}
print OUTFILE "ID\tDays Missed\tDays Observed\tAdherance\n";
foreach my $id (sort(keys(%adhash))) {
print OUTFILE "$id\t$dmhash{$id}\t$dohash{$id}\t$adhash{$id}\n";
}
Now I've created a directory and an array for all the files' names in the directory (@files)



What advice do you have for creating a loop that loops through each file and the proper way to print the merged output I mentioned that I want.

Thanks guys!!!
 
Good Memory Kevin. Thanks for keeping me from wasting my time.

Kudos and Star,
- Miller
 
Hey
I thanked Miller for giving a helpful post. He/she was right. I gave a star or whatever to thank Miller. And sorry I didn't say anything about your post Kevin but I wasn't looking for the output you gave in your code. The output I wanted was one patient id PER row and I finally figured it out. But I wanted to post something even more challenging to see if anybody knew how to approach it.
Cheers anyway,
klkot
 
I'm glad that I was able to help you out. However, you did ask:

klkot said:
Does anyone know the code to make the output print in the form I want it to?

I really appreciate it any advice ;)

If that request was no longer needed, it would have been nice if you had come back to say that you had finished. Either way, Kevin did volunteer his time to help, and that help was not acknowledged in either a negative or positive way. I'm sure that you can understand how that will only dissuade people from helping in the future.

Anyway, all that aside, maybe the following code will help you solve your current challenge.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%hash1[/blue] = [red]([/red]
	[fuchsia]1223[/fuchsia] => [fuchsia].95[/fuchsia],
	[fuchsia]1224[/fuchsia] => [fuchsia].56[/fuchsia],
	[fuchsia]1225[/fuchsia] => [fuchsia].78[/fuchsia],
	[fuchsia]1226[/fuchsia] => [fuchsia].99[/fuchsia],
	[fuchsia]1227[/fuchsia] => [fuchsia].67[/fuchsia],
[red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]%hash2[/blue] = [red]([/red]
	[fuchsia]1226[/fuchsia] => [fuchsia].85[/fuchsia],
	[fuchsia]1227[/fuchsia] => [fuchsia].66[/fuchsia],
	[fuchsia]1487[/fuchsia] => [fuchsia].45[/fuchsia],
	[fuchsia]1489[/fuchsia] => [fuchsia].99[/fuchsia],
	[fuchsia]1500[/fuchsia] => [fuchsia].32[/fuchsia],
[red])[/red][red];[/red]

[gray][i]# Obtain list of all unique keys.[/i][/gray]
[black][b]my[/b][/black] [blue]%hash3[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][blue]$_[/blue] => [fuchsia]1[/fuchsia][red]}[/red] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%hash1[/blue], [black][b]keys[/b][/black] [blue]%hash2[/blue][red];[/red]

[olive][b]foreach[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [black][b]keys[/b][/black] [blue]%hash3[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/printf.html][black][b]printf[/b][/black][/url] [red]"[/red][purple]%5s %5s %5s\n[/purple][red]"[/red], [blue]$_[/blue], [blue]$hash1[/blue][red]{[/red][blue]$_[/blue][red]}[/red], [blue]$hash2[/blue][red]{[/red][blue]$_[/blue][red]}[/red][red];[/red]
[red]}[/red]

Output:

Code:
>perl scratch2.pl
 1223  0.95
 1224  0.56
 1225  0.78
 1226  0.99  0.85
 1227  0.67  0.66
 1487        0.45
 1489        0.99
 1500        0.32

- Miller
 
I beleive my code in the other thread did what you wanted, how you wanted. The output was done using Data::Dumper instead of a loop. Here it is done with a loop:
Code:
#use Data::Dumper;
my %results = ();
readline <DATA>; #skip header
while (<DATA>) {
   chomp;
   my ($id, $dm, $do) = split(/\s+/);
   $results{$id}{TotalDM} += ($dm =~ /^(\d*)\.?$/) ? $1 : 0;
   $results{$id}{TotalDO} += ($do =~ /^(\d*)\.?$/) ? $1 : 0;
}
foreach my $id (sort {$a <=> $b } keys %results) {
   print "$id  $results{$id}{TotalDM} $results{$id}{TotalDO}\n"
}	
#print Dumper \%results;
__DATA__
ID       Intdaymiss      Daysobs
2611     .                 12
2611     5                 28
2612     .                  0
2613     0                  .
2613     5.                 5
2613     17                50

the output is:

2611 5 40
2612 0 0
2613 22 55

I assumed a value of "." could be treated as 0 (zero).
All that it needed was for you to figure out how to format the output. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Miller and Kevin
What can I say other than: you people ROCK! Thanks for the valuable advice. Both of your codes have helped me form my solution. If you want to see my final solution I would be happy to show you...
Can't thank you enough!
~klkot

two stars for the two of you :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top