[8342] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1960 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 23 17:07:25 1998

Date: Mon, 23 Feb 98 14:01:39 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 23 Feb 1998     Volume: 8 Number: 1960

Today's topics:
        Sorting that Hash on non-key field (part way there) (Laurel Shimer)
        Sorting that Hash on non-key field (part way there) (Laurel Shimer)
    Re: Sorting that Hash on non-key field (part way there) (Sean McAfee)
        test install core dump <philip@dynamind-llc.com>
    Re: The Ineffable Tom C. <jbc@west.net>
    Re: why no Case statment? <jhi@alpha.hut.fi>
        Why PERL? [Re: Beginner in trouble] <joseph@5sigma.com>
        Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 23 Feb 1998 19:32:49 GMT
From: autopen@autopen.com (Laurel Shimer)
Subject: Sorting that Hash on non-key field (part way there)
Message-Id: <autopen-2302981133550001@dynamic40.pm04.mv.best.com>

Thanks to all of you who've guided my faltering footsteps on working with
my associative array- a task you wouldn't have even had to think twice
about.

And not making me feel dumb.

1) I understand how to sort on the key fields.
2) I have been trying to sort on non-key fields, but I'm still confused -
and I must admit the $a and $b business still seems like witchcraft to me,
which is probably why I'm doing something wrong with them. Ok they are
'package globals' (in the sort package? I guess that functions are part of
some default package my perl interpreter knows about without my explicitly
telling it).

As Tom told me(1.),  those hashes are easier than I probably think...  

*After I read his message (1.), I was able to see straight enough to sort
on last name,
 .........
print "-------\nSorted by key (last name)\n";

 foreach $key (sort keys %people) {
   print " From for each line $key $people{$key}{age} $people{$key}{height} \n";
   }

 .........
* But when I attempt to sort on a non-key field (in this case age) they
are still confusing me. I see more data returned than I expect, twice as
much (shown in the log below). Half of it shows the data sorted by age (I
guess I'm getting closer) and half of  it looks like" HASH(0xbd624)" - I
guess this means it's showing me the address of something? And I'm sure
that I'm telling it to do that.

  So I know I'm still buffalowed good.

Is it something about the way I pass $a and $b ?

>From what the Camel book shows (on page 218) ,the example there has an
associative array @class which appears to have many fields, one of which
is 'age'. They use a simple $age{$a} <=> $age{$b} in their byage
subroutine. But when I try that, I just get the " HASH(0xbd624)"  style
response and never see the data I recognize.

-----------
foreach $key2 (sort  byage %people) {
   print " $key2 is $people{$key2}{age} years old and 
$people{$key2}{height} inches tall\n\n";
   }
sub byage {
   #$age{$a} <=> $age{$b};
   $people{$a}{age}<=> $people{$b}{age};
}

-----------

LOG
----
------- The whole thing plus the run ------------
#!/usr/local/bin/perl5
$mydata="sort.test";
$delim="&";

dbmopen (%mydata, $mydata, 0666) || die "$!\n";

----- (removed part where I stick the test data in)
    
 
 @associative_array_keys = keys (%people);
print "The array keys are:";
print "@associative_array_keys\n";

$countem=@associative_array_keys;
print "There are $countem keys\n";

  

for ($i=0;$i<$countem;$i++) {
   $row=$associative_array_keys[$i];
   
   print "$row is $people{$row}{age} years old and";
   print "  $people{$row}{height} inches tall \n";

   }
 

 
 
print "-------------------------------\n Now we attempt to sort\n";

 # THIS WORKS
 
print "-------\nSorted by key (last name) \n";

 foreach $key (sort keys %people) {
   print "  $key $people{$key}{age} $people{$key}{height} \n";
   }

 print "-------\nSort by age\n";


 # THIS GIVES TWICE AS MUCH OUTPUT, HALF OF WHICH IS GOOD AND THE SORT I
WOULD EXPECT

 foreach $key2 (sort  byage %people) {
   print " $key2 is $people{$key2}{age} years old and 
$people{$key2}{height} inches tall\n\n";
   }

 dbmclose (%mydata);

sub byage {
   #$age{$a} <=> $age{$b};
   $people{$a}{age}<=> $people{$b}{age};
}


elmer% perl5 sort.hash.cgi
 The array keys are:Jo Maria Manuel Teresa Varun Elena Ted Elmer Horatio
 
Jo is 56 years old and  8 inches tall
Maria is 59 years old and  5 inches tall
Manuel is 51 years old and  13 inches tall
Teresa is 57 years old and  7 inches tall
Varun is 54 years old and  10 inches tall
Elena is 58 years old and  6 inches tall
Ted is 55 years old and  9 inches tall
Elmer is 53 years old and  11 inches tall
Horatio is 52 years old and  12 inches tall
-------------------------------
 Now we attempt to sort
-------
Sorted by key (last name)
  Elena 58 6
  Elmer 53 11
  Horatio 52 12
  Jo 56 8
  Manuel 51 13
  Maria 59 5
 Ted 55 9
 Teresa 57 7
 Varun 54 10
-------
Sort by age
 HASH(0xbd4e0) is  years old and   inches tall

 HASH(0xbd534) is  years old and   inches tall

 HASH(0xb4050) is  years old and   inches tall

 HASH(0xbd624) is  years old and   inches tall

 HASH(0xbd48c) is  years old and   inches tall

 HASH(0xb8de8) is  years old and   inches tall

 HASH(0xbd498) is  years old and   inches tall

 HASH(0xbbaf0) is  years old and   inches tall

 HASH(0xeaacc) is  years old and   inches tall

 Manuel is 51 years old and  13 inches tall

 Horatio is 52 years old and  12 inches tall

 Elmer is 53 years old and  11 inches tall

 Varun is 54 years old and  10 inches tall

 Ted is 55 years old and  9 inches tall

 Jo is 56 years old and  8 inches tall

 Teresa is 57 years old and  7 inches tall

 Elena is 58 years old and  6 inches tall

 Maria is 59 years old and  5 inches tall

elmer%



(1)-------- Here is Tom's response in case anybody else out there is
trying to learn along with me

Hashes are probably easier than you think. Try running the example below.
The "key" of a hash can be any darned thing you want it to be; normally
it's a string. You can sort any array because they have an implicit order
(the array index); you can't sort a hash since their entries are by
definition unordered. 

In this example the "keys" function takes a hash as its parameter and
builds an array containing the keys; the sort function takes that array and
sorts it. What you are trying to do is just an extension of this. Remember,
you are trying to sort an array of the the keys, once they are ordered you
can use them one at time to retrieve the values by indexing into the hash. 
 

-Tom

#########################################

$people{"Millard Filmore"} = "Thirteenth President";

$people{"Abe Lincoln"}     = "Sixteenth President";

foreach $key (sort keys %people) {
   print "$key    $people{$key} \n";
}

########################################

-- 
        The Reader's Corner: Mystery, Romance, Fantasy 
         http://www.autopen.com/index.shtml 
     Subscribe to our free StoryBytes publication
 Did you miss? The Pigeon, A St.John Bathshirker Mystery
    http://www.autopen.com/pigeon.shtml


------------------------------

Date: 23 Feb 1998 20:58:35 GMT
From: autopen@autopen.com (Laurel Shimer)
Subject: Sorting that Hash on non-key field (part way there)
Message-Id: <autopen-2302981259410001@dynamic40.pm04.mv.best.com>

Thanks to all of you who've guided my faltering footsteps on working with
my associative array- a task you wouldn't have even had to think twice
about.

And not making me feel dumb.

1) I understand how to sort on the key fields.
2) I have been trying to sort on non-key fields, but I'm still confused -
and I must admit the $a and $b business still seems like witchcraft to me,
which is probably why I'm doing something wrong with them. Ok they are
'package globals' (in the sort package? I guess that functions are part of
some default package my perl interpreter knows about without my explicitly
telling it).

As Tom told me(1.),  those hashes are easier than I probably think...  

*After I read his message (1.), I was able to see straight enough to sort
on last name,
 .........
print "-------\nSorted by key (last name)\n";

 foreach $key (sort keys %people) {
   print " From for each line $key $people{$key}{age} $people{$key}{height} \n";
   }

 .........
* But when I attempt to sort on a non-key field (in this case age) they
are still confusing me. I see more data returned than I expect, twice as
much (shown in the log below). Half of it shows the data sorted by age (I
guess I'm getting closer) and half of  it looks like" HASH(0xbd624)" - I
guess this means it's showing me the address of something? And I'm sure
that I'm telling it to do that.

  So I know I'm still buffalowed good.

Is it something about the way I pass $a and $b ?

>From what the Camel book shows (on page 218) ,the example there has an
associative array @class which appears to have many fields, one of which
is 'age'. They use a simple $age{$a} <=> $age{$b} in their byage
subroutine. But when I try that, I just get the " HASH(0xbd624)"  style
response and never see the data I recognize.

-----------
foreach $key2 (sort  byage %people) {
   print " $key2 is $people{$key2}{age} years old and 
$people{$key2}{height} inches tall\n\n";
   }
sub byage {
   #$age{$a} <=> $age{$b};
   $people{$a}{age}<=> $people{$b}{age};
}

-----------

LOG
----
------- The whole thing plus the run ------------
#!/usr/local/bin/perl5
$mydata="sort.test";
$delim="&";

dbmopen (%mydata, $mydata, 0666) || die "$!\n";

----- (removed part where I stick the test data in)
    
 
 @associative_array_keys = keys (%people);
print "The array keys are:";
print "@associative_array_keys\n";

$countem=@associative_array_keys;
print "There are $countem keys\n";

  

for ($i=0;$i<$countem;$i++) {
   $row=$associative_array_keys[$i];
   
   print "$row is $people{$row}{age} years old and";
   print "  $people{$row}{height} inches tall \n";

   }
 

 
 
print "-------------------------------\n Now we attempt to sort\n";

 # THIS WORKS
 
print "-------\nSorted by key (last name) \n";

 foreach $key (sort keys %people) {
   print "  $key $people{$key}{age} $people{$key}{height} \n";
   }

 print "-------\nSort by age\n";


 # THIS GIVES TWICE AS MUCH OUTPUT, HALF OF WHICH IS GOOD AND THE SORT I
WOULD EXPECT

 foreach $key2 (sort  byage %people) {
   print " $key2 is $people{$key2}{age} years old and 
$people{$key2}{height} inches tall\n\n";
   }

 dbmclose (%mydata);

sub byage {
   #$age{$a} <=> $age{$b};
   $people{$a}{age}<=> $people{$b}{age};
}


elmer% perl5 sort.hash.cgi
 The array keys are:Jo Maria Manuel Teresa Varun Elena Ted Elmer Horatio
 
Jo is 56 years old and  8 inches tall
Maria is 59 years old and  5 inches tall
Manuel is 51 years old and  13 inches tall
Teresa is 57 years old and  7 inches tall
Varun is 54 years old and  10 inches tall
Elena is 58 years old and  6 inches tall
Ted is 55 years old and  9 inches tall
Elmer is 53 years old and  11 inches tall
Horatio is 52 years old and  12 inches tall
-------------------------------
 Now we attempt to sort
-------
Sorted by key (last name)
  Elena 58 6
  Elmer 53 11
  Horatio 52 12
  Jo 56 8
  Manuel 51 13
  Maria 59 5
 Ted 55 9
 Teresa 57 7
 Varun 54 10
-------
Sort by age
 HASH(0xbd4e0) is  years old and   inches tall

 HASH(0xbd534) is  years old and   inches tall

 HASH(0xb4050) is  years old and   inches tall

 HASH(0xbd624) is  years old and   inches tall

 HASH(0xbd48c) is  years old and   inches tall

 HASH(0xb8de8) is  years old and   inches tall

 HASH(0xbd498) is  years old and   inches tall

 HASH(0xbbaf0) is  years old and   inches tall

 HASH(0xeaacc) is  years old and   inches tall

 Manuel is 51 years old and  13 inches tall

 Horatio is 52 years old and  12 inches tall

 Elmer is 53 years old and  11 inches tall

 Varun is 54 years old and  10 inches tall

 Ted is 55 years old and  9 inches tall

 Jo is 56 years old and  8 inches tall

 Teresa is 57 years old and  7 inches tall

 Elena is 58 years old and  6 inches tall

 Maria is 59 years old and  5 inches tall

elmer%



(1)-------- Here is Tom's response in case anybody else out there is
trying to learn along with me

Hashes are probably easier than you think. Try running the example below.
The "key" of a hash can be any darned thing you want it to be; normally
it's a string. You can sort any array because they have an implicit order
(the array index); you can't sort a hash since their entries are by
definition unordered. 

In this example the "keys" function takes a hash as its parameter and
builds an array containing the keys; the sort function takes that array and
sorts it. What you are trying to do is just an extension of this. Remember,
you are trying to sort an array of the the keys, once they are ordered you
can use them one at time to retrieve the values by indexing into the hash. 
 

-Tom

#########################################

$people{"Millard Filmore"} = "Thirteenth President";

$people{"Abe Lincoln"}     = "Sixteenth President";

foreach $key (sort keys %people) {
   print "$key    $people{$key} \n";
}

########################################

-- 
        The Reader's Corner: Mystery, Romance, Fantasy 
         http://www.autopen.com/index.shtml 
     Subscribe to our free StoryBytes publication
 Did you miss? The Pigeon, A St.John Bathshirker Mystery
    http://www.autopen.com/pigeon.shtml


------------------------------

Date: Mon, 23 Feb 1998 19:59:09 GMT
From: mcafee@seawolf.rs.itd.umich.edu (Sean McAfee)
Subject: Re: Sorting that Hash on non-key field (part way there)
Message-Id: <hykI.2573$N4.16095547@news.itd.umich.edu>

In article <autopen-2302981133550001@dynamic40.pm04.mv.best.com>,
Laurel Shimer <autopen@autopen.com> wrote:
<snip>
>* But when I attempt to sort on a non-key field (in this case age) they
>are still confusing me. I see more data returned than I expect, twice as
>much (shown in the log below). Half of it shows the data sorted by age (I
>guess I'm getting closer) and half of  it looks like" HASH(0xbd624)" - I
>guess this means it's showing me the address of something? And I'm sure
>that I'm telling it to do that.

I missed your original article, but it looks like this:

>foreach $key2 (sort  byage %people) {

 ...should be "sort byage keys %people".  Without the "keys", you're
sorting the keys AND values of the hash, all mixed together.  The values,
being references, are stringified in the manner you describe.  This
explains why you're seeing twice as much data as you expect.

HTH.

--
Sean McAfee | GS d->-- s+++: a25 C++ US+++$ P+++ L++ E- W+ N++ |
            | K w--- O? M V-- PS+ PE Y+ PGP?>++ t+() 5++ X+ R+ | mcafee@
            | tv+ b++ DI++ D+ G e++>++++ h- r y+>++**          | umich.edu


------------------------------

Date: Mon, 23 Feb 1998 14:39:26 -0500
From: "Philip S. Wachtel" <philip@dynamind-llc.com>
Subject: test install core dump
Message-Id: <34F1D06E.81F220AC@dynamind-llc.com>

Hello,

Does anyone know why I would be getting a core dump when running 'make
test' for a perl 5.004_04 installation on a Solaris 2.5 machine?  The
 ./configure and make went fine with no errors.  But halfway through the
make test, I get a core dump on the file com/redef.t.  I don't want to
do the rest of the install until I know what this means.  I would be
grateful for the help.

Thanks in advance,
Philip



------------------------------

Date: Mon, 23 Feb 1998 13:04:00 -0800
From: John Callender <jbc@west.net>
Subject: Re: The Ineffable Tom C.
Message-Id: <34F1E440.BAC6FD17@west.net>

Robert Melson wrote:

> Like him or loathe him or something in between, Tom has knowledge and
> experience that few others participating in this group can equal.  I
> would suggest to you that putting up with what many consider to be
> Tom's abrasive personality is a small enough price to pay for the
> value returned and that you should hope that the current round of
> villification directed toward him will not -- again -- drive him from
> active participation in this newsgroup.

Okay. I hope that.

At the same time, the "value" of Tom's participation in the group for a
given user varies greatly depending on that user's level of
sophistication. For newbies, sarcastic one-liners, even if they include
references to the appropriate manpages, probably do more harm than good
(unless one defines driving newcomers away from the group as a "good").

This isn't to imply that Tom has not contributed mightily to the
development of the language, or that he doesn't have the right to be as
snide as he wants to in this newsgroup, or that making public fun of
newbies isn't a valid form of humorous expression that is worth keeping
around just so the old timers can have a few yucks. He has, and he does,
and it is.

But he's a big boy. If he wants to rip folks whose main failing is that
they haven't been around long enough to discover the appropriate
resources to answer their own questions, that's his right. And if some
others of us want to comment negatively on his being needlessly cruel to
people who, for all their ignorance, still bleed when you prick them,
that's our right, too.

I doubt that this sort of "vilification" would ever "drive him from the
group." If he did leave, though, the loss of his insights that the
advanced users would feel would be balanced somewhat by the elimination
of some knee-jerk newbie bashing.

Personally, I'd rather see him stay. But if he can't take even this much
heat, he shouldn't be in the kitchen -- and he certainly shouldn't be
the one turning up the flames on those most susceptible to being burned.

--
John Callender
jbc@west.net
http://www.west.net/~jbc/


------------------------------

Date: 23 Feb 1998 23:03:08 +0200
From: Jarkko Hietaniemi <jhi@alpha.hut.fi>
Subject: Re: why no Case statment?
Message-Id: <oeeg1lahw4z.fsf@alpha.hut.fi>


Tzadik Vanderhoof <stvhoof@netvision.net.il> writes:
> Why doesn't Perl have a real Case statement?  I've read what the Camel
> book says about the "alternatives", but none of them are really as nice
> as a real Case statement supported directly by the language.  I realise
> that this is something that probably only Larry could answer, and I've
> heard he no longer sees this newsgroup, but maybe someone has some ideas
> about this...

The basic problem is _which_ case statement should Perl support?

The exact numerical one of C?
The filename globbing one of /bin/sh?
The numerical + ranges of Pascal?  (IIRC, it's been a long time)

or more generally,

	numbers or strings?
	exact matching or some pattern matching?
	comparison operators? (less than et al)

If you can answer these questions, well, then you can write the
if-elseif-else.

-- 
$jhi++; # http://www.iki.fi/~jhi/
        # There is this special biologist word we use for 'stable'.
        # It is 'dead'. -- Jack Cohen


------------------------------

Date: Mon, 23 Feb 1998 14:30:14 -0700
From: "Joseph N. Hall" <joseph@5sigma.com>
Subject: Why PERL? [Re: Beginner in trouble]
Message-Id: <34F1EA49.2CCA32BB@5sigma.com>

Something I've always wondered is where people get the
uppercase PERL thing from.  Any ideas?

	-joseph

Alberto Turon Lanuza wrote:
> 
> Hi, I4m trying to write my first PERL script, [..]

-- 
Joseph N. Hall, prop., 5 Sigma Productions       mailto:joseph@5sigma.com
Author, Effective Perl Programming . . . . . http://www.effectiveperl.com
Perl Training  . . . . . . . . . . . . . . .  http://www.perltraining.com


------------------------------

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V8 Issue 1960
**************************************

home help back first fref pref prev next nref lref last post