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

My problem is as follows: I have a double hash called 'quantum' which references arrays. One hash is for an i.d. number, while the other is for a date. (So each quantum{$id}{$date} is an array organized by id and date). When I try to run my code, it stops at the same line each time, complaining about my use of a string (the date) as a HASH ref. It seems to complain about different date strings from my .csv file each time I run it, if that helps. It also says the error occurred at the final line of the .csv file. The second foreach loop in the code below is the line of code where the error is found. I've omitted what's inside the loops since it doesn't seem to affect the error.

foreach $id (sort keys %quantum) { foreach $date ( sort {$a->{int(substr($quantum{$id},2,2))} <=> $ +b->{int(substr($quantum{$id},2,2))} || $a->{int(substr($quantum{$id},0,2))} <=> $b->{int(substr($q +uantum{$id},0,2))} } keys %{$quantum{$id}}) { } }

Replies are listed 'Best First'.
Re: Can't use string as a HASH ref
by no_slogan (Deacon) on May 16, 2014 at 16:52 UTC
    sort { substr($a,2,2) <=> substr($b,2,2) || ... }
      That worked for me, thanks! So simple haha
Re: Can't use string as a HASH ref
by RichardK (Parson) on May 16, 2014 at 17:01 UTC

    keys %{quantum{$id}} returns a list of key names not references, so $a->{...} doesn't make any sense in that sort. Have a look at the help for keys for an example of how to sort by key. I'm not sure what you are trying to do , so I can't suggest how to fix it. How about showing us some sample data and what results you expect?

Re: Can't use string as a HASH ref
by kennethk (Abbot) on May 16, 2014 at 17:10 UTC
    Let's decompose what you are doing:
    foreach $id (sort keys %quantum) {
    Okay, so $id contains a string which is also a key in %quantum. Next:
    foreach $date ( sort ... keys %{$quantum{$applid}}) { }
    Okay, now we're going to sort a bunch of keys from a contained hashref. I note that you have $applid instead of $id. Is this a typo, or is this some value imported from elsewhere? Ignoring this fact, your sort looks like:
    sort { $a->{int(substr($quantum{$id},2,2))} <=> $b->{int +(substr($quantum{$id},2,2))} || $a->{int(substr($quantum{$id},0,2))} <=> $b->{int +(substr($quantum{$id},0,2))} }
    The things being fed into your sort (your $a and $b) are the keys (strings), not the values (hash refs). You are trying to dereference the hash keys as hashes and thus your error. You likely meant
    foreach my $id (sort keys %quantum) { foreach my $date ( sort { int(substr($quantum{$id}{$a},2,2)) <=> int(substr +($quantum{$id}{$b},2,2)) || int(substr($quantum{$id}{$a},0,2)) <=> int(substr +($quantum{$id}{$b},0,2)) } keys %{$quantum{$id}}) { } }
    Or maybe no_slogan guessed better. In either case, see HASHES OF HASHES in perldsc, and maybe perlreftut to boot.

    I'm also a little thrown as to why strict is not complaining about you not using my on your loop variables, but that's a different question.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Sorry, applid is a typo, it's suppose to be id which i just changed in my post for better readability. If I sort according to your suggestion, would I not be sorting the keys (dates) by values contained within their arrays? I want to sort by the name of the key (date) itself, for example the $date value "05162014" should be sorted to come after "05152014". Thanks for your comments.

        Which is why you are supposed to post input data, so that we don't have to guess. See How do I post a question effectively?.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.