in reply to Help with complex data structures

1. Is is safe to use a variable variable name for the hash (%$uid)?

No, don't do this. See Why it's stupid to `use a variable as a variable name' for detailed explanation (Dominus writes better than I do ;--)

3. Would you do it another way?

Use a hash of hashes, the uid being the key in the first hash:

# store the values $files{$uid}={ name => $name, size => $size, age => $age}; # use them, print uid/name foreach my $uid (keys %files) { print "$uid: $files{$uid}->{name}\n"; }

Replies are listed 'Best First'.
Re: Re: Help with complex data structures
by tommyw (Hermit) on Nov 14, 2002 at 15:42 UTC

    Use a hash of array of hashes, so that we can remember more than one file per user:

    # store the values push @{$files{$uid}}, { name => $name, size => $size, age => $age}; # use them foreach my $uid (keys %files) { print "$uid: files are:\n"; foreach my $file (@{$files{$uid}}) { print " $file->{name}\n"; } }

    Updated: D'oh. Must remember to braces around the key when dereferencing $file->{name} (rather than $file->name)

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      print " $file->name\n"; produces output like this:

      HASH(0x80ed0cc)->name
      HASH(0x80ed0fc)->name
      HASH(0x80ed180)->name
      HASH(0x80ee984)->name
      etc...

      Update: check that. The syntax $file->{name} should be used.

      Update again! Thanks for everyone's input. I have a first draft of the program here if you are interested.

      Neil Watson
      watson-wilson.ca