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!

add consecutive missing keys in a hash 1

Status
Not open for further replies.

klkot

Programmer
Joined
Jun 6, 2007
Messages
18
Location
US
Hi again Perl gurus.
I've used the following code which produces the output I want where patientid is the key and each file has three values so the output looks like:
Patientid Var1File1 Var2File1 Var3File1 Var1File2......
1200 val val val val
1203 val val val val
1204 val val val val

Now I want to use my following code to insert patientid key values where there was a consecutive missing patientid. Like
Patientid Var1File1 Var2File1 Var3File1 Var1File2....
1200 val val val val
1201 nil nil nil nil
1202 nil nil nil nil
1203 val val val val
1204 val val val val

This is the exact code of mine that worked on producing my first dataset correctly. Now I just don't know how to add consecutive missing keys.... If any gurus know, could you please let me know? THANKya

foreach $id (sort(keys(%megahash))) {
my %patient_data = %{$megahash{$id}};

print OUTFILE "$id";
foreach my $file (sort(@files)) {
print OUTFILE ",";
if(exists $patient_data{$file}) {
print OUTFILE join(",", @{$patient_data{$file}});
} else {
print OUTFILE "nil,nil,nil";
}
}

print OUTFILE"\n";
}
 
any chance you know what the patientid should start and end at all the time or is it always different?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I know patientid should go from 1101-1241 consecutively but there happen to be missing observations for patientid in the actual dataset. I want to add the missing rows.

Thanks Kevin!
 
if you know the ID range beforehand, you can build your initial dataset first and add/change data as you process the data from your input:

Code:
my %patient = ();
foreach my $id (1101..1241) {
   $patient{$id}=[('nill')x3];
}

or loop through the range of IDs and add them after the fact, although this will probably be less efficient:

Code:
foreach my $id (1101..1241) {
   next if exists $patien{$id};
   $patient{$id}=[('nill')x3];
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin
I hope you win for Perl-meister of the week so I gave you a star! You make hard things easy and you've been a great help so far. I might have to post again if I can't get it to go through properly tomorrow but it looks like it'll work
Cheers Kevin ;)
klkot
 
If the patientid is a number I would use an array of hashes of arrays, and, as it starts at 1101, correct the index so unnecessary memory space is not wasted; also you get automatic sorting.
So:
Code:
  #dimension the array
$#megarray=1241-1101;
  #fill the array
my$file;
...
  @{$megarray[$patientid-1101]{$file}}=split;
    #(assuming space separated data in the source files)
...
  #print the array
for($i=0;$i<=1241-1101;$i++){
 print OUTFILE $i+1101;
 for$file(sort@files){
  print OUTFILE',';
  if(exists$megarray[$i]{$file}){
   print OUTFILE join(',',@{$megarray[$i]{$file}});
  }else{
   print OUTFILE'nil,nil,nil';
  }
 }
 print OUTFILE"\n";
}


prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
$#megarray = 1241-1101-1; [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
but you don't have ids 3-4, you have ids 1101-1241.

1241-1101 = 140 (elements)

index 0 thru 140 is 141 elements in length. The array needs to be 140 elements in length which is index 0 thru 139.

So I confirm:

1241-1101-1 [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry again, but if you have id's 1101-1241 (of course including both ends, unless specified otherwise) the number of elements is 141, not 140.
My example is the method I use to avoid this kind of very common mistakes: take a very short array and test your code by hand. With id's 3 and 4 it is immediate that you have two elements, not one (=4-3).

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks prex1.
I managed to use kevin's first code and it seemed to work but I see yours is very good and advanced (maybe a little too advanced for me;)
Thanks as well prex1
~klkot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top