Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Accessing values of a Hash object

by dReKurCe (Scribe)
on Mar 13, 2007 at 20:59 UTC ( [id://604682]=perlquestion: print w/replies, xml ) Need Help??

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

Greets Monks, Just a little rusty after an extended hiatus: Looking to return the hash value here? Can anyone point out my error and suggest a fix? Thanks Monks.
#! /usr/bin/perl package ngetlogstdin; sub new { my $self={}; $self->{o}="ngeterr.log"; $self->{a}=undef; $self->{i}={[(110,FB),(100,F)]}; $self->{F}=undef; $self->{B}=$url; bless $self,ngetlogstdin; return $self; } sub access { $self=shift; $arg=shift; print $arg; }
The interface:
#! /usr/bin/perl use ngetlogstdin; $obj=new ngetlogstdin; for $key(keys %$obj){print "$key\n"}; $arg=<stdin>; $value=$obj->access($arg); print $value;

Replies are listed 'Best First'.
Re: Accessing values of a Hash object
by ikegami (Patriarch) on Mar 13, 2007 at 21:06 UTC
    There is a lot of room for improvements (use of lexical variables instead of package variables, elimination of constant 2nd arg for bless, elimination of indirect method invocation), but the problem is that $arg has a newline.

    Change
    $arg=<stdin>;
    to
    chomp($arg=<stdin>);

    Ref: chomp, IO Operators

    Update: Oh and change
    print $arg;
    to
    return $arg;

Re: Accessing values of a Hash object
by GrandFather (Saint) on Mar 13, 2007 at 21:14 UTC
    1. use strictures (use strict; use warnings;).
    2. $self->{i}={[(110,FB),(100,F)]}; does not make sense. What do you intend?
    3. Where are you looking to return a hash value?

    The following reworked sample code may give you some hints:

    use strict; use warnings; package ngetlogstdin; sub new { my $self={}; $self->{o}="ngeterr.log"; $self->{a}=undef; $self->{i}={110 => 'FB', 100 => 'F'}; $self->{F}=undef; $self->{B}='http://wibble.wobble.com'; bless $self; return $self; } sub access { my $self=shift; my $arg=shift; print $arg; # Note that "result" of print is the return value } 1; package main; my $obj=new ngetlogstdin; for my $key(keys %$obj){print "$key\n"}; my $arg='tomatoes'; my $value=$obj->access($arg); print $value;

    Prints:

    F a B i o tomatoes1

    DWIM is Perl's answer to Gödel
      Ok, Well I guess what I'm asking is how to get 'at' the hash from the accessor. I want to prompt for the key then print the value.

        so you want something like:

        sub access { my ($self, $arg) = @_; return $self->{$arg}; } ... my $arg='B';

        Prints (using previous sample code):

        F a B i o http://wibble.wobble.com

        DWIM is Perl's answer to Gödel
Re: Accessing values of a Hash object
by talexb (Chancellor) on Mar 13, 2007 at 21:11 UTC

    And I'll say the same thing I said to you earlier in the CB .. run your code in the debugger, and you'll find your error.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Accessing values of a Hash object
by davorg (Chancellor) on Mar 14, 2007 at 09:00 UTC
    $self->{i}={[(110,FB),(100,F)]};

    This is almost certainly not doing what you think it's doing.

    You take two lists - (110, FB) and (100, F) - and flatten them into a single list - (110, FB, 100, F). You then create an anonymous array that contains this list. You then create an anonymous hash where the (single) key is the stringified reference to your anonymous array. There is no value associated with this key.

    Adding "use strict" and "use warnings" to your code will really help you to track down problems like this.

      Hi, Thanks for drawing attention to this syntax. Won't the value to the stringified key be in fact the anon array. That is the one flattened list. Its here that i thought I could do something like:
      $self->{i}={[(110,FB),(100,F)]} ... $objinstance=Class::package(); $value=$objinstance->{i}; for (@$value){..do some code with (110,FB) and (100,F)}
Re: Accessing values of a Hash object
by dReKurCe (Scribe) on Mar 13, 2007 at 21:30 UTC
    Right ...gotch Grand Father the return value... rusty rusty rusty

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604682]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found