Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comparing ARRAY Ref values

by filipebean (Novice)
on May 09, 2013 at 13:32 UTC ( #1032768=perlquestion: print w/ replies, xml ) Need Help??
filipebean has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Iím printing the ref value of 2 arrays and the ref value is different, but the arrays values are equal (Iíve printed all the values inside and are the same).

Is it ok or am I missing something (assuming the content is the same):

Array 1: ARRAY(0x8020b2f8) Array 2: ARRAY(0x801db638)

Both values also change every time I run the script, I thought It was the memory value so it should have the same value always Ö

Thank you in advance.

Best regards.

Comment on Comparing ARRAY Ref values
Select or Download Code
Re: Comparing ARRAY Ref values
by InfiniteSilence (Curate) on May 09, 2013 at 13:47 UTC

    Maybe share some code? Normally your refs, if pointing to the same array, should show the same thing when you print them:

    perl -e '@arr = qw|a b c d|; $ref = \@arr; $ref2 = \@arr; print qq|$r +ef\t$ref2\n|;'

    At least for this run shows:

    ARRAY(0x82b1ee4) ARRAY(0x82b1ee4)

    Celebrate Intellectual Diversity

Re: Comparing ARRAY Ref values
by ww (Bishop) on May 09, 2013 at 13:48 UTC
    You're definitely missing something. InfiniteSilence has addressed your second question one of your questions (yes, I used to get confused about 'left' and 'right' sometimes, too.

    As to the first other, the values you're looking at are -- to all intents and purposes -- pointers to the memory locations (addresses) where the arrays begin. They are NOT, IN ANY SENSE reflective of the values in the arrays themselves.

    Update: Demo:

    #!/usr/bin/perl use 5.016; my @arr1 = qw(a b c d e); my @arr2 = (3, 5, 7, 9); my $ref1 = \@arr1; my $ref1a = \@arr1; my $ref2 = \@arr2; say "\$ref1: $ref1 and \$ref1a: $ref1a"; say "\$ref2: $ref2; \@$ref2: @$ref2;" #Second half DEREFs the referen +ce =head demo C:\>1032768-refs.pl $ref1: ARRAY(0x119ee3c) and $ref1a: ARRAY(0x119ee3c) $ref2: ARRAY(0x119f12c); @ARRAY(0x119f12c): 3 5 7 9;

    See the tuts here at References.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Hi,

      this is similar of what I have:

      #!/usr/bin/perl my @arr1=(); push(@arr1,3); push(@arr1,5); push(@arr1,7); push(@arr1,9); my @arr2 = (3, 5, 7, 9); my $ref1 = \@arr1; my $ref2 = \@arr2; print "\$ref1: $ref1 \n"; print "\$ref2: $ref2";

      result:

      $ perl reftes.pl $ref1: ARRAY(0x80071d38) $ref2: ARRAY(0x80071e40)

      As you said, it points to the memory locations (addresses) where the arrays begin and it has no relation with the values inside.

      Thank you

Re: Comparing ARRAY Ref values
by greengaroo (Hermit) on May 09, 2013 at 14:42 UTC

    If the addresses of the two references are different, it means it points to two different memory space. Comparing the references in scalar context you will only compare the address, if you want to compare the content, here is a suggestion:

    use Test::Deep::NoTest qw( eq_deeply ); if ( eq_deeply($ref1, $ref2) ) { print "Identical!\n"; } else { print "Different!\n"; }

    It works for complex structures as well (deep hashrefs including mixes of hashrefs and arrayrefs).

    In your case, if the two references must point to the same structure, then you have a bug in your code that creates two references to two separate memory space. Now why do they have the same content, that depends on your code.

    A for will get you from A to Z; a while will get you everywhere.
Re: Comparing ARRAY Ref values
by tobyink (Abbot) on May 09, 2013 at 15:50 UTC

    If I have a shopping list with "bread, milk, butter" written on it, and you have a shopping list with "bread, milk, butter" on it, then we still have two different shopping lists. The way we can prove that they're different shopping lists is if I add "jam" to my list, your list is unaffected.

    So what you're doing is comparing whether the shopping lists are the same shopping list. What you (seem to) want to do is compare whether they contain the same items.

    Test::Deep::NoTest as suggested by greengaroo is a good place to start. Also read the documentation for set and bag from the same module to allow a little more control over the comparison (e.g. whether the order of items in the arrays is significant; whether duplicates are significant).

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032768]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-12-28 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls