Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How to compare an empty key with another empty key in associative array

by nandymamith (Novice)
on Oct 24, 2012 at 10:33 UTC ( [id://1000596]=perlquestion: print w/replies, xml ) Need Help??

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

Am trying to compare two values, say $a = ''; $b = {}; if($a eq $b){ print "PASS"; } else { die "FAIL"; } May i know how to get the output as PASS ?? moreover i don't want get my out put as FAIL. In short how to compare the associative array($b) with the empty variable ($a)

  • Comment on How to compare an empty key with another empty key in associative array

Replies are listed 'Best First'.
Re: How to compare an empty key with another empty key in associative array
by bart (Canon) on Oct 24, 2012 at 11:06 UTC
    That sounds like you're coming from PHP. Well, OK, I'll write a little utility function empty which should look a bit familiar. That way you can compare if 2 values are both empty.
    sub empty { my $r = shift; if(!ref $r) { return !$r } if(ref $r eq 'HASH') { return !keys %$r } if(ref $r eq 'ARRAY') { return !@$r } if(ref $r eq 'SCALAR') { return !$$r } return !1; } $a = ''; $b = {}; $c = []; if(empty($a) && empty($b) && empty($c)) { print "PASS"; }
Re: How to compare an empty key with another empty key in associative array
by DrHyde (Prior) on Oct 24, 2012 at 10:41 UTC
    '' and {} are different in just about every possible way. What you are trying to do is equivalent to comparing a strawberry and a moose and say that they're the same.
Re: How to compare an empty key with another empty key in associative array
by 2teez (Vicar) on Oct 24, 2012 at 12:58 UTC

    Hi nandymamith,

    • $a = ''; $b = {};
      You really don't want to use $a and $b which are aliases for the two operands being compared in a sortsub.
    • That been said, $a and $b in above are two different things in perl. While $a is an empty scalar variable, $b is a reference to an anonymous hash, created using curly brackets which is empty in this case

    • However, in this case, you can compare these, printing what you want like so:
      use warnings; use strict; my $empty_str = q{}; my $empty_hash_ref = { q{} => q{}, }; for ( keys %$empty_hash_ref ) { $empty_str eq $_ ? print "PASS" : print "FAIL"; ## print PASS } ## use smartmarch ~~ $empty_str ~~ $empty_hash_ref ? print "PASS" : print "FAIL"; ## print +PASS
    • Note however, that iterating through the hash comparing either the keys or values of the hash with the empty scalar variable, when the hash is empty ( with no variable atall) will print nothing, but using the smart match ~~ Operator will print "FAIL"
    • .
      my $empty_str = q{}; my $empty_hash_ref = {}; ## why? for (keys %$empty_hash_ref){ $empty_str eq $_? print "PASS": print "FAIL"; # print nothing } $empty_str~~$empty_hash_ref?print "PASS": print "FAIL"; # print FAI +L

    Hope this helps

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: How to compare an empty key with another empty key in associative array
by Rudolf (Pilgrim) on Oct 24, 2012 at 11:50 UTC

    $b = {}; is not an associative array. An array containing other arrays may look something like this:

    my @two_d_array = ([$x,$y,$z],[1,2,3],['a','b','c']);

    To see if an array is empty, you can check it's scalar value for contents.

    my $elements = scalar(@two_d_array);

    UPDATED: Associative array is considered a hash in Perl, (thanks choroba),

      "Associative array" is "hash" in Perl lingo, not an array.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found