slloyd has asked for the wisdom of the Perl Monks concerning the following question:

How do I extract the keys out of a Win32::OLE hash? If I print the variable that holds a Win32::OLE hash, called $hash, I get Win32::OLE=HASH(0x22ad4c) . But the following code does not return anything.
my @keys=keys(%{$hash}); print "Keys: @keys\n";

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com

Replies are listed 'Best First'.
Re: keys in a Win32::OLE Hash
by ikegami (Patriarch) on Nov 10, 2005 at 18:52 UTC

    More info please? It works for me:

    use strict; use warnings; use Win32::OLE (); my $ex = Win32::OLE->new('Excel.Application') or die("Unable to open Excel object\n"); foreach my $key (keys(%$ex)) { print($key, "\n"); # Application # Creator # Parent # ActiveCell # ActiveChart # ActivePrinter # ... }

    Or if you want to to iterate over a collection returned by a OLE object, use the in function provided by Win32::OLE.

Re: keys in a Win32::OLE Hash
by GrandFather (Saint) on Nov 10, 2005 at 19:37 UTC
    use strict; use warnings; use Win32::OLE; my $hashRef = Win32::OLE->new('Excel.Application') or die "Can't open +Excel"; my @keys = keys %{$hashRef}; print "@keys\n";

    Prints (in part):

    Application Creator Parent ActiveCell ActiveChart ActivePrinter Active +Sheet ActiveWindow ActiveWorkbook Assistant Cells Charts CommandBars +DDEAppReturnCode Names Range Selection Sheets ThisWorkbook

    Is there soemthing else you aren't telling us? Are you using use strict; use warnings;? Do you check that your Win32::OLE->new succeded?


    Perl is Huffman encoded by design.
Re: keys in a Win32::OLE Hash
by planetscape (Chancellor) on Nov 10, 2005 at 18:28 UTC

        Absent your actual data, my best suggestion is probably to "print" $hash using something like:

        use Data::Dumper; ... print Dumper( $hash );

        This way, you can see what you have on your hands and get an idea of the syntax needed to work with it.

        intro to references and References quick reference are probably also required reading. :-)

        HTH,

        planetscape