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

Hi, I'd seen my array returning the value like HASH(0x67890809)(for example), but I am not sure why. :(

I would expect the value to be some meaningful value like 0 or 1, is there any way that i can point my array to the data instead of hash value which seems like a memory address to me :-/

anyway...my array is something like this:

@array = XMLP::GetElementsByName($node, $path)

and if I loop the below code, it doesnt gives me the output that i wanted, unless i take out the sort.

foreach $variable(sort @array) { $id= XMLP::Getelementbyvalue($variable); }

I am not interested to dive into the xml files to look at the function but I am very curious why a "sort" can somehow different from without sorting the array if the $variable return the hash value.

Replies are listed 'Best First'.
Re: why does array return the hash value?
by graff (Chancellor) on Apr 10, 2010 at 19:08 UTC
    I doubt that anyone here knows what XMLP::GetElementsByName() returns (XMLP doesn't seem to exist on CPAN). If you don't have your own docs to explain what it returns, you should use Data::Dumper to look at what's in @array:
    use Data::Dumper 'Dumper'; ... @array = XMLP::GetElementsByName($node, $path) print Dumper( \@array );
    Assuming that it's some sort of array (list) of objects, your use of "sort" is almost certainly not doing what you expect or want it to do. It's more likely you'll need to provide a sort block that compares elements of the structures returned by that module function.
      Thanks for the pointer! will try that on!
Re: why does array return the hash value?
by JavaFan (Canon) on Apr 10, 2010 at 14:36 UTC
    If $variable contains a hashreference, it means @array contains a hashreference. It's not that @array is one.

    I don't know the function XMLP::GetElementsByName, but I wouldn't be surprised if it returns an object - and the object is implemented as a hashref.

Re: why does array return the hash value?
by CountZero (Bishop) on Apr 11, 2010 at 07:14 UTC
    Even without exactly knowing what the XMLP module does, one may safely assume that the GetElementsByName method returns a list of objects.

    If you iterate over them in a for loop, $variable will get set to the next object and the Getelementbyvalue will know what to do with it.

    However if you sort the list of objects, poor sort has no clue what to do with the object, so by default it will stringify the object, indeed giving you something like HASH(0x67890809) as most objects are implemented as references to a hash. Unfortunately, stringifying an object actually destroys the objectness of it, or rather the result of stringifying an object is no longer an object.

    Does it mean that one cannot sort objects? Of course not, but a naive sort is not going to be able to do it. At the very least you will have to find out on basis of which attribute of the object you will have to do the sort and then write a subroutine which you can give to sort to use.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks for the explanation! it helps on my understanding!
Re: why does array return the hash value?
by noobnoob (Initiate) on Apr 10, 2010 at 15:22 UTC
    Emm...Here is how the code looks like and i would like the output to be "my_sub0_sub1" but because @array pass the hash value into the $variable and sometimes it would cause the output become "my_sub1_sub0". But removing the sort in foreach loop seems to solve this issue, which makes me puzzle:/
    my $style ="my_"; foreach$variable (sort @array){ my $id = XMLP::GetElementAttribute($variable,"ID"); $sub .= "_sub" ; $sub .= $id ; } $style .= $sub;