[JaxPM] syslog analyzing

Steve Lane sml at zfx.com
Mon Apr 10 17:37:55 CDT 2000


On the jacksonville-pm-list; Jax.PM'er Steve Lane <sml at zfx.com> wrote -

j proctor wrote:
> 
> On the jacksonville-pm-list; Jax.PM'er j proctor <jproctor at oit.umass.edu> wrote -
> 
> > foreach $hash (%postfix, %mars, %jupiter, %szebra, %wwwsaigon,
> >                 %sol, %mercury, %zeus ) {
> >    while(($key, $val)=(each $hash )){
> >
> [...]
> >
> > Type of arg 1 to each must be hash (not scalar deref) at test-sort.pl line
> > 140, near "$hash )"
> > test-sort.pl had compilation errors.
> 
> I think, because you're trying to assign a list of hashes in turn to a
> scalar, that foreach is actually giving you a reference to each hash
> instead.  Since I'm pretty sure foreach %hash... won't work, what you need
> to do is tell the each that you're giving it a hash ref instead of an
> actual hash.  Well, more precisely, to dereference it back to a hash.
> Try this:
> 
>     while(($key, $val)=(each %{$hash} )){

this is wrong.  $hash is -not- a hash reference, it's a key or value
from the hashes in the foreach () that got "smoothed out" to a
plain list.  that's basic perl: arrays and hashes inside a list
lose their array- or hash-identity and all the values are merged
into a single list.

what you need to do is make sure that $hash is a hashref.
you can do this is two ways:

   # backslash each hash
   foreach $hash (\%postfix, \%mars, \%jupiter, \%szebra, \%wwwsaigon,
                  \%sol, \%mercury, \%zeus) {

   # backslash the entire list of hashes, which works because
   # backslashing a list is distributive
   foreach $hash (\(%postfix, %mars, %jupiter, %szebra, %wwwsaigon,
                    %sol, %mercury, %zeus)) {

P.S. the error "Type of arg 1 to each must be hash (not scalar deref)"
may be confusing here.  you get this error if you use each() on
any scalar, not just references:

   $ perl -e '$a = 1; while (each $a) { }'
   Type of arg 1 to each must be hash (not scalar deref) at -e line 1,
near "$a) "
   Execution of -e aborted due to compilation errors.

   $ perl -e '$a = {}; while (each $a) { }'
   Type of arg 1 to each must be hash (not scalar deref) at -e line 1,
near "$a) "
   Execution of -e aborted due to compilation errors.

the error would probably be improved if it said just "scalar"
instead of "scalar deref" in appropriate situations, but i'm
not excited enough about that to complain to P5P.
--
Steve Lane <sml at zfx.com>

Jax.PM Moderator's Note:
This message was posted to the Jacksonville Perl Monger's Group listserv.
The group manager can be reached at -- owner-jacksonville-pm-list at pm.org
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list