[24559] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6737 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jun 27 18:05:39 2004

Date: Sun, 27 Jun 2004 15:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 27 Jun 2004     Volume: 10 Number: 6737

Today's topics:
    Re: Printing a hash question <postmaster@castleamber.com>
        Sorting an associative array <nolson66@comcast.net>
    Re: Sorting an associative array <BZ@caradhras.net>
    Re: Sorting an associative array <postmaster@castleamber.com>
    Re: Sorting an associative array <postmaster@castleamber.com>
    Re: Sorting an associative array <noreply@gunnar.cc>
    Re: Sorting an associative array <noreply@gunnar.cc>
    Re: Sorting an associative array <postmaster@castleamber.com>
    Re: Sorting an associative array <postmaster@castleamber.com>
    Re: Sorting an associative array <noreply@gunnar.cc>
    Re: Sorting an associative array <noreply@gunnar.cc>
    Re: Sorting an associative array <postmaster@castleamber.com>
    Re: Sorting an associative array <krahnj@acm.org>
    Re: Sorting an associative array <noreply@gunnar.cc>
    Re: Sorting an associative array <krahnj@acm.org>
        undefined subroutine that is defined?  mob_perl / perl  <dburke210@comcast.net>
        win32 odbc test script <jcharth@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 27 Jun 2004 12:29:27 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Printing a hash question
Message-Id: <40df03fa$0$190$58c7af7e@news.kabelfoon.nl>

Eric Bohlman wrote:

> John Bokma <postmaster@castleamber.com> wrote in 

>>for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
>>
>>}

> It's a bad idea to try to answer Frequently Asked Questions yourself rather

Sometimes postings like the above are added to the FAQ, or can be a 
correction on the FAQ.

OTOH I agree with you too ;-)

> the process he made a mistake.  Another regular posted a correction, but

No problem there. I see often corrections and/or additions by people 
here, wether it are FAQ questions or not. It keeps one sharp, and one 
learns.

> it's quite possible that the original poster, not to mention any number of 
> lurkers, stopped reading the thread as soon as they saw what looked like an 
> answer and missed the correction (the correction may not have made it to 
> their news server yet).

So they have a non-working solution. Either they correct it themself and 
learn from it or they come back for more.

You mistake Usenet with a free helpdesk and solution providing thingy.

> Someone who asks FAQs is, almost by definition, someone who doesn't yet 
> know enough about Perl to be able to tell whether or not he's getting a bum 
> steer.  There's very little chance that the Perl FAQ will give them a bum 
> steer.

Someone who just copy-pastes code will run into many problems. Wether 
the FAQ is the source or not. The FAQ will rarely provide what they 
exactly want.

> There's a much greater chance that a Usenet poster will 
> accidentally do so.

And nothing wrong there. The answers are given for free.

I see that there is a sorting question ;-)

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 10:01:49 -0700
From: Nathan Olson <nolson66@comcast.net>
Subject: Sorting an associative array
Message-Id: <BD044B8D.1397A%nolson66@comcast.net>

I've got an associative array whose keys are movie titles. I'd like to sort
the keys in an order that takes into account titles beginning with "A" or
"The." In other words, "The Graduate" ought to be sorted with the 'g's, not
the 't's. Is there any way to do this that doesn't involve sorting manually?


Thanks in advance,
Nate Olson



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

Date: 27 Jun 2004 17:26:48 GMT
From: BZ <BZ@caradhras.net>
Subject: Re: Sorting an associative array
Message-Id: <slrncdu0qo.6ld.BZ@kalypso.caradhras.net>

Nathan Olson wrote in comp.lang.perl.misc:
>  I've got an associative array whose keys are movie titles. I'd like to sort
>  the keys in an order that takes into account titles beginning with "A" or
>  "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
>  the 't's. Is there any way to do this that doesn't involve sorting manually?

Something like this should work:

sort { 
	$a =~ s/^(a|the)\s+//; 
	$b =~ s/^(a|the)\s+//; 
	$a <=> $b 
} keys %hash;

-- 
BZ


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

Date: Sun, 27 Jun 2004 12:34:35 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Sorting an associative array
Message-Id: <40df052c$0$196$58c7af7e@news.kabelfoon.nl>

Nathan Olson wrote:

> I've got an associative array whose keys are movie titles. I'd like to sort
> the keys in an order that takes into account titles beginning with "A" or
> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
> the 't's. Is there any way to do this that doesn't involve sorting manually

create a look up table (array) consisting of arrays with the first 
element the title, and the second one the title with "A " and "The " etc 
removed. Sort it on the *second* element.

Next use the first element to index your assoc array.

Google for 'Schwartzian Transform' for some nice examples.
for example: http://www.stonehenge.com/merlyn/UnixReview/col06.html

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 12:37:24 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Sorting an associative array
Message-Id: <40df05d6$0$196$58c7af7e@news.kabelfoon.nl>

BZ wrote:

> Nathan Olson wrote in comp.lang.perl.misc:
> 
>> I've got an associative array whose keys are movie titles. I'd like to sort
>> the keys in an order that takes into account titles beginning with "A" or
>> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
>> the 't's. Is there any way to do this that doesn't involve sorting manually?
> 
> Something like this should work:
> 
> sort { 
> 	$a =~ s/^(a|the)\s+//; 
> 	$b =~ s/^(a|the)\s+//; 
> 	$a <=> $b 
> } keys %hash;

Which does O(n log n) replacements. It might be faster to create a 
look-up table, with O(n) replacements, and use that to sort.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 19:33:37 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting an associative array
Message-Id: <2k8f01F1983kaU1@uni-berlin.de>

Nathan Olson wrote:
> I've got an associative array whose keys are movie titles. I'd like
> to sort the keys in an order that takes into account titles
> beginning with "A" or "The." In other words, "The Graduate" ought
> to be sorted with the 'g's, not the 't's.

     my @sortedtitles = sort {
         ($a =~ /(?i:the|a)?\s*(.+)/)[0]
                       cmp
         ($b =~ /(?i:the|a)?\s*(.+)/)[0]
     } keys %movies;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 27 Jun 2004 19:41:22 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting an associative array
Message-Id: <2k8fejF18s0ddU1@uni-berlin.de>

Gunnar Hjalmarsson wrote:
> 
>     my @sortedtitles = sort {
>         ($a =~ /(?i:the|a)?\s*(.+)/)[0]
>                       cmp
>         ($b =~ /(?i:the|a)?\s*(.+)/)[0]
>     } keys %movies;

Correction: Make that

     my @sortedtitles = sort {
         ($a =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
                       cmp
         ($b =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
     } keys %movies;

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 27 Jun 2004 12:49:44 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Sorting an associative array
Message-Id: <40df08bb$0$195$58c7af7e@news.kabelfoon.nl>



Gunnar Hjalmarsson wrote:

> Nathan Olson wrote:
> 
>> I've got an associative array whose keys are movie titles. I'd like
>> to sort the keys in an order that takes into account titles
>> beginning with "A" or "The." In other words, "The Graduate" ought
>> to be sorted with the 'g's, not the 't's.
> 
> 
>     my @sortedtitles = sort {
>         ($a =~ /(?i:the|a)?\s*(.+)/)[0]

This fails (?) with Thesomething and Asomething. Yeah, I know that the 
specs said A and The., but you missed the dot after The too, so that's 
not an excuse :-D

It also strips spaces in front of titles starting with spaces (ok there 
probably are none like that)

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 12:53:33 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Sorting an associative array
Message-Id: <40df099d$0$195$58c7af7e@news.kabelfoon.nl>

Gunnar Hjalmarsson wrote:

> Gunnar Hjalmarsson wrote:
> 
>>
>>     my @sortedtitles = sort {
>>         ($a =~ /(?i:the|a)?\s*(.+)/)[0]
>>                       cmp
>>         ($b =~ /(?i:the|a)?\s*(.+)/)[0]
>>     } keys %movies;
> 
> Correction: Make that
> 
>     my @sortedtitles = sort {
>         ($a =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>                       cmp
>         ($b =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>     } keys %movies;

"Someone and the thingy"

(don't you need ^ ?)

And how to index %movies? I guess that the OP needs to access the info 
in the %movies assoc.

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 20:04:22 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting an associative array
Message-Id: <2k8gpnF18n94sU1@uni-berlin.de>

John Bokma wrote:
> Gunnar Hjalmarsson wrote:
>>
>>     my @sortedtitles = sort {
>>         ($a =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>>                       cmp
>>         ($b =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>>     } keys %movies;
> 
> "Someone and the thingy"
> 
> (don't you need ^ ?)

No. Unless a key starts with 'A ' or 'The ', (.+) captures the whole 
key. (But ^ wouldn't have hurted, for the sake of clarity...)

> And how to index %movies? I guess that the OP needs to access the
> info in the %movies assoc.

Not sure what you mean. @sortedtitles can now be used to access the 
info in %movies in the desired order.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 27 Jun 2004 20:59:42 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting an associative array
Message-Id: <2k8k1jF1923ecU1@uni-berlin.de>

BZ wrote:
> Nathan Olson wrote in comp.lang.perl.misc:
>> I've got an associative array whose keys are movie titles. I'd
>> like to sort the keys in an order that takes into account titles
>> beginning with "A" or "The." In other words, "The Graduate" ought
>> to be sorted with the 'g's, not the 't's. Is there any way to do
>> this that doesn't involve sorting manually?
> 
> Something like this should work:
> 
> sort {
> 	$a =~ s/^(a|the)\s+//;
> 	$b =~ s/^(a|the)\s+//;
> 	$a <=> $b
> } keys %hash;

Did you try it?

- It does not replace case insensitively.
- It sorts strings numerically.

Besides that, since all the elements in the returned list are no
longer an (exact) key in the hash, how would you use the list?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 27 Jun 2004 14:34:54 -0500
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Sorting an associative array
Message-Id: <40df2169$0$877$58c7af7e@news.kabelfoon.nl>

Gunnar Hjalmarsson wrote:
> John Bokma wrote:
> 
>> Gunnar Hjalmarsson wrote:
>>
>>>
>>>     my @sortedtitles = sort {
>>>         ($a =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>>>                       cmp
>>>         ($b =~ /(?:(?i:the|a)\s+)?(.+)/)[0]
>>>     } keys %movies;
>>
>>
>> "Someone and the thingy"
>>
>> (don't you need ^ ?)
> 
> No. Unless a key starts with 'A ' or 'The ', (.+) captures the whole 
> key. (But ^ wouldn't have hurted, for the sake of clarity...)

Ah, grmbl, indeed.

>> And how to index %movies? I guess that the OP needs to access the
>> info in the %movies assoc.
> 
> Not sure what you mean. @sortedtitles can now be used to access the info 
> in %movies in the desired order.

Indeed, my mistake again, it's a match :-(

-- 
John                               MexIT: http://johnbokma.com/mexit/
                            personal page:       http://johnbokma.com/
    Experienced Perl programmer available:     http://castleamber.com/
             Happy Customers: http://castleamber.com/testimonials.html


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

Date: Sun, 27 Jun 2004 19:46:32 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Sorting an associative array
Message-Id: <40DF2403.A6C73E55@acm.org>

Nathan Olson wrote:
> 
> I've got an associative array whose keys are movie titles. I'd like to sort
> the keys in an order that takes into account titles beginning with "A" or
> "The." In other words, "The Graduate" ought to be sorted with the 'g's, not
> the 't's. Is there any way to do this that doesn't involve sorting manually?

my @sorted_movie_titles =
    map { s/^[^\0]+\0//; $_ }
    sort
    map { (my $x = $_) =~ s/^(?:a|the)\s*//i; "$x\0$_" }
    keys %hash;



John
-- 
use Perl;
program
fulfillment


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

Date: Sun, 27 Jun 2004 21:59:55 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting an associative array
Message-Id: <2k8nikF18rkr2U1@uni-berlin.de>

John W. Krahn wrote:
> 
>     map { (my $x = $_) =~ s/^(?:a|the)\s*//i; "$x\0$_" }
------------------------------------------^

Seems as if you made a mistake similar to the one I made in my first post.

     map { (my $x = $_) =~ s/^(?:a|the)\s+//i; "$x\0$_" }


-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 27 Jun 2004 21:23:16 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Sorting an associative array
Message-Id: <40DF3AAF.9E118A4E@acm.org>

Gunnar Hjalmarsson wrote:
> 
> John W. Krahn wrote:
> >
> >     map { (my $x = $_) =~ s/^(?:a|the)\s*//i; "$x\0$_" }
> ------------------------------------------^
> 
> Seems as if you made a mistake similar to the one I made in my first post.
> 
>      map { (my $x = $_) =~ s/^(?:a|the)\s+//i; "$x\0$_" }

Ah yes, thanks.


John
-- 
use Perl;
program
fulfillment


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

Date: Sun, 27 Jun 2004 14:12:31 -0400
From: "Dan Burke" <dburke210@comcast.net>
Subject: undefined subroutine that is defined?  mob_perl / perl 5.8.4 issue?
Message-Id: <BPOdnSFE5NeYk0LdRVn-jw@comcast.com>

Hello,

We've hit a problem where we've started getting an error from mod_perl about
undefined subroutines.  I know for a fact it is defined, but we've been
unable to trace the cause of the error.

The error seems to be perl 5.8.4 and mod_perl related (both 1 and 2), but
only on linux (we have 4 Solaris 9 systems running perl 5.8.4 not having
issues).

This is but one of meany errors:

error_log:[Sun Jun 27 10:25:49 2004] [error] Undefined subroutine
&gccDispCustomAcctData::CS_Get_Account_Data called at
/opt1/prod/http/cgi-bin/bus/srv_cs_ntt/gccDispCustomAcctData.pm line 182,
<DATA> line 225.\n

I checked gccDispCustomAcctData.pm, and it gets CS_Get_Account_Data from
gccGetAccountData.pm, which is "used" in gccDispCustomAcctData.pm, and is
properly exported.

We initially got this error on a test linux server running RedHat 7.3, which
we had upgraded perl to 5.8.4 and installed Apache2 and mod_perl2 for
testing.  We gave up on that and grabbed a Sun box that was already loaded,
because we assumed that that test box was just such and old and abused OS
load that it was naturally going to give problems.  Now we have upgraded a
linux box to Fedora C2, installed 5.8.4, apache 1.3.31 and mod_perl 1.29,
and we get the errors.

I hope I'm making sense.  The breakdown basically is:

Same machine (Dell 1550, before and after reloading OS):
RedHat 7.3, perl 5.6.1, apache 1.3.29, mod_perl 1.27 - *NO ERROR*
Fedora Core 2, perl 5.8.4, apache 2, mod_perl 1.99_14 - *ERROR*

Two other machines:
RedHat 7.3, perl 5.8.4, apache 1.3.31, mod_perl 1.29 - *ERROR*  (Dell 1650)
Solaris 9, perl 5.8.4, apache 2, mod_perl 1.99_14 - *NO ERROR*  (ultra60)

Using exact same versions of the code on all instances.

It always hits on the same functions as undefined (varios functions,
different files).  If I try an older version of the code it picks a
different set of functions to always hit on for the error.

Any help would be greatly appreciated.

Dan
dburke210 (at) comcast.net










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

Date: Sun, 27 Jun 2004 15:51:06 GMT
From: joe <jcharth@hotmail.com>
Subject: win32 odbc test script
Message-Id: <Xns951577CEEACD9josephthecianet@207.69.154.202>

Hi i just wrote a little quick and dirty script to query odbc, i could not 
find anything similar  so i wrote this.

use Win32::ODBC;
print "Enter ODBC data source: ";
$myprompt = <STDIN>;
chop($myprompt);

my($db) = new Win32::ODBC("$myprompt");

while (true){
print "> ";
$myprompt = <STDIN>;
chop($myprompt);

if ($myprompt eq "quit"){
$db->Close();
exit;
}

$db->Sql("$myprompt");
my(@fields) = $db->FieldNames;

while ($db->FetchRow()) {
my(%data) = $db->DataHash();
foreach $field (sort @fields) {
	$test = "$data{$field} ";
      print "$test";
      }		
   print "\n";

	}
   }
	


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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.

#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 V10 Issue 6737
***************************************


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