Hamish Whittal <[EMAIL PROTECTED]> wrote:
: 
: I have a hash that looks like:
: 
: { number => {somethings in here} }
: 
: Now I need to get at number, not what it references.

my %hash = ( number => {} );

foreach my $key ( keys %hash ) {
    # do something with each key ($key)
}

    Since your hash has only one key, this will
loop just one time. Don't get trapped into the
thinking that there is a first (or second, or
last) key. Arrays and list are ordered. Hashes
are not. If you write code assuming keys are
ordered, your code may break when changed
later or if a newer version of perl changes a
hashing algorithm.


HTH,

Charles K. Clarkson
-- 
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %hash = ( aa => 3 );
print initial_key( \%hash ), "\n";

my @keys = 'e' .. 'n';
@hash{ @keys } = 1 .. @keys;
print initial_key( \%hash ), "\n";

delete $hash{m};
print initial_key( \%hash ), "\n";

@keys = 'e' .. 'z';
@hash{ @keys } = 1 .. @keys;
print initial_key( \%hash ), "\n";

sub initial_key {
        return ( keys %{ $_[0] } )[0];
}

__END__





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to