Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Error "Can't use an undefined value as a HASH reference" while working with generated objects

by McA (Priest)
on Oct 11, 2012 at 09:29 UTC ( [id://998400]=note: print w/replies, xml ) Need Help??


in reply to Error "Can't use an undefined value as a HASH reference" while working with generated objects

my %valeur = defined($values[0][0]) && ref($values[0][0]) eq 'HASH' ? +%{$values[0][0]} : ();

Probably somthing like this?

Best regards
McA

  • Comment on Re: Error "Can't use an undefined value as a HASH reference" while working with generated objects
  • Download Code

Replies are listed 'Best First'.
Re^2: Error "Can't use an undefined value as a HASH reference" while working with generated objects
by HJO (Acolyte) on Oct 11, 2012 at 09:41 UTC

    Hi McA,

    Waow, thanks it worked perfectly, but could you just please explain it to me ^^" I'm unused to your use of : ? %{$values[0][0]} : ();

      my %valeur; if( @values and defined $values[0] and 'ARRAY' eq ref $values[0] and defined $values[0][0] and 'HASH' eq ref $values[0][0] ) { %valeur = %{ $values[0][0] }; }

      my %valeur; if ( my $want = Dive( \@values, qw/ 0 0 / ){ %valeur = %$want; }

        The Data::Diver example will die if $values[0][0] is defined and true but not a hashref - e.g. if it's an arrayref.

        use Data::Diver 'Dive'; use Scalar::Does 'does'; if (my $want = Dive( \@values, qw/ 0 0 / )) { %valeur = %$want if does($want, 'HASH'); }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Waow thanks, I really didn't push the defined so far ...

        I just tested it (the first method, I'm having some trouble with your second, some undeclared varibles...), It's working as well, thanks

      Hi!

      Sorry for the late reply: Please have a look at http://perldoc.perl.org/perlop.html#Conditional-Operator . This operator is also known as triadic operator. It's a kind of if-then-else-operator.

      Hope it helps

      Best regards
      McA

        Hi,

        Not a problem, I just though you didn't see my reply ^^" Well thanks, it's a little easier to understand now with some documentation.

        Thanks again for your answers

        Regards, HJO

Log In?
Username:
Password:

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

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

    No recent polls found