Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by McA (Curate)
on Oct 11, 2012 at 09:29 UTC ( #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
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; }

        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

        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'

      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
Node Status?
node history
Node Type: note [id://998400]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-10-01 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (38 votes), past polls