in reply to Looping Hash keys to get excel wks using Win32::OLE does not work

The keys of (non-magical) hashes are always strings. Therefore, $iSheet contains a string, the stringification of the sheet number. Because $iSheet contains a string, you're telling Excel to return the Worksheet *named* "3" instead of the Worksheet *at index* 3. Convert $iSheet to a number.

my $sheet = $oBook->Worksheets->{0+$iSheet};

Better yet, use a HoA instead of a HoH. You never use the hash value, and I don't think you're using it to get rid of dups.

HoH (current):

if ($name =~ /Header/) { $H_TEST{"HEADER"}{$iSheet} = 1; } if ($name =~ /ALO/) { $H_TEST{"DATA"}{$iSheet} = 1; } ... foreach (sort keys %{$H_TEST{$tag}}) { my $iSheet = 0 + $_;

HoA (proposed):

if ($name =~ /Header/) { push @{$H_TEST{"HEADER"}}, $iSheet; } if ($name =~ /ALO/) { push @{$H_TEST{"DATA"}}, $iSheet; } ... foreach my $iSheet (sort @{$H_TEST{$tag}}) {

Replies are listed 'Best First'.
Re^2: Looping Hash keys to get excel wks using Win32::OLE does not work
by uvs (Novice) on Oct 19, 2007 at 16:57 UTC
    Thank you!!!!!!! It worked like a charm. Before posting i was planning to convert $iSheet to numeric and then test it out but thought that might not be the problem. Turns out that was the problem.

    Thank you very much for prompt reply. It made my day.