> -----Original Message----- > From: Jensen Kenneth B SrA AFPC/DPDMPQ > [mailto:[EMAIL PROTECTED]] > Sent: Thursday, December 26, 2002 9:46 AM > To: '[EMAIL PROTECTED]' > Subject: Formatting output > > > Accidentally sent before I was done writing. > > I am trying to iterate through two hashes and print each > key/value. In one > column the key/value from one hash and another column the > key/values of the > other hash. So the output would look something like this > > Some header | header for column 2 > Key value key value > Key value key value > Key value key value > Key value key value > Key value key value > Key value key value > Key value > Key value > > Etc... > > Here's what I am doing right now > > ($a, $b, $c) = 0; > foreach (sort keys (%TARGET)){ > @TEMP[$a] = "$_ $TARGET{$_}"; > $a++; > } > foreach (sort keys (%PROD)){ > @TEMP2[$b] = "$_ $PROD{$_}"; > $b++; > } > print OUT "\@ prod not present. | Present not at prod.\n"; > if ($a > $b){ > for ($n=0 , $n = $a, $n++){ > printf OUT "%15s %15s\n", (@TEMP[$n], @TEMP2[$n]); > } > } else { > for ($n=0 , $n = $b, $n++){ > printf OUT "%15s %15s\n", (@TEMP[$n], @TEMP2[$n]); > } > } >
Perl's real good at iterating. You might try something like this: my (@TEMP, @TEMP2); push @TEMP, "$_ $TARGET{$_}" for sort keys %TARGET; push @TEMP2, "$_ $PROD{$_}" for sort keys %PROD; printf OUT "%-15.15s %-15.15s\n", shift @TEMP, shift @TEMP2 while @TEMP || @TEMP2; That last line will throw some warnings about undefined values if you're using -w. You can suppress those with (Perl 5.6 or higher): { no warnings 'undefined'; printf OUT "%-15.15s %-15.15s\n", shift @TEMP, shift @TEMP2 while @TEMP || @TEMP2; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]