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

Hello, I've googled the error in my Title, but still can't seem to get it. I have the following code:
$tmp_string="SARSTATS$SAROPT"; $rec=$$tmp_string[$SAR_DATA_START]{ $tmp_array[0] }; $rec->{device}=$tmp_array[0]; push (@{$rec->{date}}, $date_time); push (@{$rec->{busy}}, $tmp_array[1]);
Which works like a champ on Solaris (2.6, 2.7, 2.8) and aix (4.3.3 & 5L), but not on HP-UX (9000 or 11.00). They are all running 5.6.1 of Perl. Please smile favorably upon me Monks! gratci, Guy

Replies are listed 'Best First'.
Re: Can't coerce array into hash at
by bronto (Priest) on Mar 21, 2003 at 15:20 UTC
    $tmp_string="SARSTATS$SAROPT"; $rec=$$tmp_string[$SAR_DATA_START]{ $tmp_array[0] };

    Why are you using a scalar variable like a reference?

    Are you useing strict and warnings?

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
Re: Can't coerce array into hash at
by Elian (Parson) on Mar 21, 2003 at 16:11 UTC
    When you use an array reference as a hash reference, you trigger perl's pseudo-hash code. This only works in some versions of perl (5.6.x and 5.8.x, IIRC, though it's a dead feature) and only if the zeroeth element of the array referenced is a hash reference.

    Check to make sure you're actually using the right version of perl everywhere, and that the array is set up to act as a pseudohash.

    If you're not purposely using perl's pseudohash feature, then your program is a bug and your reference doesn't refer to what you think it does. (And you're using symbolic refs anyway, which is mildly naughty)

Re: Can't coerce array into hash at
by robartes (Priest) on Mar 21, 2003 at 16:04 UTC
    You get this error when you try to use an array ref as a hashref:
    $ perl -e '$aryref= [ qw(one two) ];$aryref->{'camel'}='flea-ridden';' Can't coerce array into hash at -e line 1.
    As you do not specify at which line the error occurs, I have to guess. There are two places where you deref a hashref (and thus two hasrefs that could actually be arrayrefs): in the second and third line of your script. The culprits are either $$tmp_string[$SAR_DATA_START] or $rec.

    As to why this should work on one OS, and not on another - I would have a long hard look at your input data: you are appear to be using sar output, which tends to be OS specific.

    As an aside, you might consider refactoring your data structure. It is quite complicated at the moment (I had to use some paper & pencil drawings to figure it out) and you are appear to be using symbolic references, which is usually not a good sign.

    Update: Stopped pretending I have psychic powers.

    CU
    Robartes-