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

Can you tell me what is wrong with this to put a username into a hash? (I know that $columns[0] is being populated with a username)
$users{$columns[0]}=x;
I then wanted to do a keys to get the unique usernames back out. (This is in a loop, so there will be many)...

Cheers.

Replies are listed 'Best First'.
Re: Assigning to a hash
by impossiblerobot (Deacon) on Dec 03, 2001 at 22:08 UTC
    If you're actually using x as a bareword in the assignment, that could be your problem.

    You should probably use either:
    $users{$columns[0]}='x';
    or
    $users{$columns[0]}=1;
    (Unless I'm misunderstanding something.)

    Impossible Robot
      Thanks. Works now!
(shockme) Re: Assigning to a hash
by shockme (Chaplain) on Dec 03, 2001 at 22:02 UTC
    I may be really missing the boat, but I thought the correct way to access a hash of arrays is:

    $users{$columns}[0] = "x";

    If things get any worse, I'll have to ask you to stop helping me.

Re: Assigning to a hash
by MZSanford (Curate) on Dec 03, 2001 at 21:57 UTC
    Assuming $columns[0] is a user name , this should work fine. I would imagine, that either $columns[0] is not populated correctly, or something more siniter is afoot ... something like a \r or \n on the username. I think adding a print above this so you can check $columns[0] would be a good idea. Also, if you show some more code, we might be able to help more.
    i had a memory leak once, and it ruined my favorite shirt.
      The perl debugger shows $columns[0] as 'willa', so I don't think any special chars are lurking...
        Is this data being read from a file ? a database? Knowing where it comes from may help me formulate a guess. What you have should work fine. If you do something like the following , does it work any better (not that it should) :
        my $key = $columns[0]; $hash{$key} = 1;

        Also, you may want to use Data::Dumper to print out the data structure to find out what is getting created.
        $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
        Weird. If $columns[0] is defined correctly, you'd think that would work. Maybe posting more code would help.
        Also, check here for other ways to Return a Unique Array.
        Rich36
        There's more than one way to screw it up...

Re: Assigning to a hash
by Fastolfe (Vicar) on Dec 04, 2001 at 00:20 UTC

    Please describe the nature of the problem you are experiencing. You've given us code (or is it pseudo-code?), but you haven't actually told us what problems you're seeing with it. Is it not compiling? Is a lookup for that username later failing to give you the result you're expecting? If the responses above don't hint at the problem, consider providing us with a little more information.

    Some simple debugging could also go a long way here:

    use Data::Dumper; print "\$columns[0] = '$columns[0]'\n"; $users{$columns[0]} = 'x'; # or whatever print Dumper(\%columns); # give us a nice view of %columns