[24951] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7201 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 2 14:06:57 2004

Date: Sat, 2 Oct 2004 11:05:09 -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           Sat, 2 Oct 2004     Volume: 10 Number: 7201

Today's topics:
        Can't compile (perlcc) both Net::FTP and Config::IniFil <usenet@m-me.dk>
        CGI PROBLEM <joechoi@sympatico.ca>
    Re: CGI PROBLEM <noreply@gunnar.cc>
    Re: CGI PROBLEM (wana)
    Re: How to merge .wav files <soon.the.sp@mmers.and.evil.ones.will.bow-down-to.us>
    Re: Is it possible to embed perl inside a shell script  (zzapper)
        Is PHP still slower than Perl? <asdf@asdfsadf.com>
    Re: Is PHP still slower than Perl? <news_comp.lang.perl.misc@canopus.nl>
    Re: Is PHP still slower than Perl? <Joe.Smith@inwap.com>
    Re: Is PHP still slower than Perl? <jw@jwscripts.com>
    Re: Is PHP still slower than Perl? <postmaster@castleamber.com>
    Re: making easy things difficult: @a-@b <tassilo.von.parseval@rwth-aachen.de>
    Re: making easy things difficult: @a-@b (wana)
    Re: making easy things difficult: @a-@b (wana)
    Re: making easy things difficult: @a-@b (Peter Corlett)
    Re: modeling an object many to many relationship with a (Topmind)
    Re: replace string in file <Joe.Smith@inwap.com>
    Re: Return top-N of Hashes - hash splice? <jurgenex@hotmail.com>
    Re: Return top-N of Hashes - hash splice? <ewijaya@singnet.com.sg.removethis>
    Re: Return top-N of Hashes - hash splice? <ewijaya@singnet.com.sg.removethis>
    Re: Return top-N of Hashes - hash splice? <jurgenex@hotmail.com>
    Re: Return top-N of Hashes - hash splice? <someone@example.com>
    Re: Return top-N of Hashes - hash splice? <Joe.Smith@inwap.com>
    Re: Return top-N of Hashes - hash splice? <jurgenex@hotmail.com>
    Re: Return top-N of Hashes - hash splice? <shawn.corey@sympatico.ca>
    Re: Return top-N of Hashes - hash splice? <tadmc@augustmail.com>
    Re: Scripting trouble <Joe.Smith@inwap.com>
    Re: Syntax appears inconsistent - why is this? <someone@example.com>
    Re: Syntax appears inconsistent - why is this? <jwkenne@attglobal.net>
    Re: While query <pinyaj@rpi.edu>
    Re: While query <pinyaj@rpi.edu>
    Re: While query <usa1@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 02 Oct 2004 15:32:44 +0100
From: Thomas Watson Steen <usenet@m-me.dk>
Subject: Can't compile (perlcc) both Net::FTP and Config::IniFiles at the same time
Message-Id: <415ebc29$0$109$65c69314@mercury.nildram.net>

I am using Cygwin on a Windows XP box. I'm running perlcc from the 
Cygwin shell. I am using perl v5.8.5 built for 
cygwin-thread-multi-64int. The command I am executing to compile is 
"perlcc -o tst.exe tst.pl".

When trying to compile a perl file only containing the following two use 
statements the compiler (perlcc) just runs for what seems forever 
(filling up huge amounts of memory):

use Net::FTP;
use Config::IniFiles;

When compiling a file the only contains one of the above use statements 
the compile completes sucessfully.

Any ideas on how to get arround this?

/watson


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

Date: Fri, 1 Oct 2004 23:51:29 -0700
From: "miem kuen choi cheung" <joechoi@sympatico.ca>
Subject: CGI PROBLEM
Message-Id: <UBp7d.30773$tT2.2008863@news20.bellglobal.com>

how to write a perl cgi for keep track the value of a form in html page, so 
that none of the field in the form will be blank? 




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

Date: Sat, 02 Oct 2004 11:27:54 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI PROBLEM
Message-Id: <2s7apbF1eqjt4U1@uni-berlin.de>

miem kuen choi cheung wrote:
> how to write a perl cgi for keep track the value of a form in html
> page, so that none of the field in the form will be blank?

Have the script test just that after parsing the form data.

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


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

Date: 2 Oct 2004 05:18:32 -0700
From: ioneabu@yahoo.com (wana)
Subject: Re: CGI PROBLEM
Message-Id: <bf0b47ca.0410020418.531a6345@posting.google.com>

"miem kuen choi cheung" <joechoi@sympatico.ca> wrote in message news:<UBp7d.30773$tT2.2008863@news20.bellglobal.com>...
> how to write a perl cgi for keep track the value of a form in html page, so 
> that none of the field in the form will be blank?

Are you using CGI.pm?  I definitely recommend Dr. Lincoln Stein's book
on it.  I had to order it on Amazon because the local book stores
don't carry it.

then, for example, a text field is written like this:

print textfield(-name=>'myfield', -value=>'my default text');

The text field will now show 'my default text' when you first load the
form.  If the form is reloaded, the default behavior is for the
textfield to retain it's previous value ('stickiness') which is a
feature but it can cause headaches if you forget it.  Make it forget
it's previous value like this:

print textfield(-name=>'myfield', -override=>1, -value=>'my default
text');

or

print textfield(-name=>'myfield', -force=>1, -value=>'my default
text');

I like to place override after name for some reason, but being an
anonymous hash, the order is not important.

Sorry if I told you something you already knew.  I am the biggest Perl
newbie ever so it's hard to believe I could teach anyone anything new
at this point, but I'm working on it.


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

Date: Sat, 2 Oct 2004 11:44:57 -0400
From: FLEB <soon.the.sp@mmers.and.evil.ones.will.bow-down-to.us>
Subject: Re: How to merge .wav files
Message-Id: <15nznpssl8tev$.7ruizd8r7puk$.dlg@40tude.net>

Regarding this well-known quote, often attributed to Jarson's famous "Thu,
30 Sep 2004 14:44:45 -0400" speech:

> I'm building a web-based message alert system in Perl (CGI) using voice TTS. 
> Each web client will get a custom voice message that will actually consist 
> of selected .wav files merged together to appear as one.  My problem, is 
> that I don't know how to handle .wav files to merge them properly under 
> Perl.  Alternatively, if there is a way for a CGI program to send a stream 
> of multiple separate .wav files, that would work to.  Is there?
> 
> Thanks, Jarson
> 
> jarson can be found at sygration. That's a dot com company.

If you wanted to use MP3s, all you have to do is concatenate them.

-- 
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
   (put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com


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

Date: 2 Oct 2004 05:00:44 -0700
From: david@tvis.co.uk (zzapper)
Subject: Re: Is it possible to embed perl inside a shell script say bash?
Message-Id: <f677762.0410020400.1b375698@posting.google.com>

William Ahern <william@wilbur.25thandClement.com> wrote in message news:<fj2k22-jnt.ln1@wilbur.25thandClement.com>...
> zzapper <david@tvisnospam.co.uk> wrote:
> > Hi,
> > Many years ago I used to embed AWK into a shell script, I've tried doing
> > this with Perl with limited results
> 
> #!/bin/sh
> 
> function exec_perl {
> 	perl <<-'EOP'
> 		sub do_print {
> 			print <<'EOF'
> 				This is printed from perl.
> 			EOF
> 		}
> 
> 		&do_print;
> 	EOP
> 
> 	cat <<-EOL
> 		This is printed from the shell.
> 	EOL
> }
> 
> exec_perl;

And the best way to pass parameters to the Perl?

(sorry to be picky)

zzapper


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

Date: Sat, 2 Oct 2004 03:56:28 -0400
From: "@" <asdf@asdfsadf.com>
Subject: Is PHP still slower than Perl?
Message-Id: <hOSdnRvNILPWwsPcRVn-jw@rogers.com>

A benchmark in 2002 showed PHP is much slower in shell or when Apache has
Mod_Perl.

With the new PHP kissing Java's ass, Perl is once again the #1 CGI choice.

Java is for a big team in short time to develope something slow.

ASP is a joke.

PHP is a kid.

Perl is around.

C. I don't know why it is not popular. It should be. Maybe those Web
inventors always thought in Linux so they never wanted to push something
binary so they selected Perl, a slower scripting.




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

Date: Sat, 2 Oct 2004 08:27:53 +0000 (UTC)
From: Daniel Tryba <news_comp.lang.perl.misc@canopus.nl>
Subject: Re: Is PHP still slower than Perl?
Message-Id: <cjloq9$3i9$1@news.tue.nl>

In comp.lang.php @ <asdf@asdfsadf.com> wrote:
> A benchmark in 2002 showed PHP is much slower in shell or when Apache has
> Mod_Perl.
> 
> With the new PHP kissing Java's ass, Perl is once again the #1 CGI choice.

If you want to troll, please put a little more effort in it.

Like make up some benchmarks to support your facts.

-- 

  Daniel Tryba



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

Date: Sat, 02 Oct 2004 11:41:50 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Is PHP still slower than Perl?
Message-Id: <2uw7d.96052$wV.30196@attbi_s54>

@ wrote:

> C. I don't know why it is not popular.

I'd say it is because C does not have strings as a native data type.
Other than initializing an array of char, the C compiler has no
string manipulation built in; it requires library functions to do that.
	-Joe


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

Date: Sat, 2 Oct 2004 16:00:42 +0200
From: "Janwillem Borleffs" <jw@jwscripts.com>
Subject: Re: Is PHP still slower than Perl?
Message-Id: <415eb484$0$76532$b83b6cc0@news.euronet.nl>

@ wrote:
> With the new PHP kissing Java's ass, Perl is once again the #1 CGI
> choice.
>

You mean this regarding PHP 5's OO model? You have never looked at Perl 6's 
OO model, haven't you?


JW





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

Date: 2 Oct 2004 16:58:07 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Is PHP still slower than Perl?
Message-Id: <Xns957679BFC204Bcastleamber@130.133.1.4>

"@" <asdf@asdfsadf.com> wrote in news:hOSdnRvNILPWwsPcRVn-jw@rogers.com:

> A benchmark in 2002 showed PHP is much slower in shell or when Apache
> has Mod_Perl.
> 
> With the new PHP kissing Java's ass, Perl is once again the #1 CGI
> choice. 

For me Perl is #1 (if possible) because PHP is probably the worst designed 
language ever. I am not sure if you can already prepare statements (MySQL), 
something I used in Perl ages ago, or still have to use all those 
whatsamacalled_quote_shebang garbage.


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


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

Date: Sat, 2 Oct 2004 07:46:18 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: making easy things difficult: @a-@b
Message-Id: <2s6tlcF1iit5iU1@uni-berlin.de>

Also sprach wana:

> Suppose I want to create an array made up of all of the elements of @a
> except for those that are also contained in @b.  For example:
> 
> (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
> 
> I was hoping that @c = @a - @b; would work.  This would really be
> making an easy task easy.

I'm not convinced that this is such a great idea.

> Searching the faqs and docs, the solution seems to be a confusing (to
> me at least) use of either a hash or grep.  I looked at the map
> function which seemed appropriate, but I couldn't think of an obvious
> way to do it.

It may not seem obvious why a hash is often the tool of choice when
manipulating lists. However, a hash has two very useful properties. One
is that you cannot have duplicate keys. This is really just a
side-effect from the way hashes work internally. The other one is the
fact that a look-up happens in constant time. 

For your problem, both features come in very handy. You'd first fill all
the values of @b into a hash as keys:

    my %hash;
    @hash{ @b } = ();

Now you use %hash as a filter. You walk through @a and only keep those
values that are not in %hash. This is efficient because a key look-up
takes next to no time:

    my @new = grep !exists $hash{$_}, @a;

Note that the values associated with each key in the hash are irrelevant
here.

In a smilar fashion hashes can be used to filter out duplicates or
calculate the intersection of two lists (in which case you'd just have
to negate the grep-condition in the above statement).
    
>  I finally settled for the old C way of doing it of having a loop in a
> loop to compare each value of one array against each value of the
> other.

You can do that, sure, but it becomes impractical for large lists.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 2 Oct 2004 05:06:35 -0700
From: ioneabu@yahoo.com (wana)
Subject: Re: making easy things difficult: @a-@b
Message-Id: <bf0b47ca.0410020406.148cf89f@posting.google.com>

"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> wrote in message news:<2s6tlcF1iit5iU1@uni-berlin.de>...
> Also sprach wana:
> 
> > Suppose I want to create an array made up of all of the elements of @a
> > except for those that are also contained in @b.  For example:
> > 
> > (apple, banana, peach, pear) - (apple, peach) = (banana, pear)
> > 
> > I was hoping that @c = @a - @b; would work.  This would really be
> > making an easy task easy.
> 
> I'm not convinced that this is such a great idea.
> 
> > Searching the faqs and docs, the solution seems to be a confusing (to
> > me at least) use of either a hash or grep.  I looked at the map
> > function which seemed appropriate, but I couldn't think of an obvious
> > way to do it.
> 
> It may not seem obvious why a hash is often the tool of choice when
> manipulating lists. However, a hash has two very useful properties. One
> is that you cannot have duplicate keys. This is really just a
> side-effect from the way hashes work internally. The other one is the
> fact that a look-up happens in constant time. 
> 
> For your problem, both features come in very handy. You'd first fill all
> the values of @b into a hash as keys:
> 
>     my %hash;
>     @hash{ @b } = ();
> 
> Now you use %hash as a filter. You walk through @a and only keep those
> values that are not in %hash. This is efficient because a key look-up
> takes next to no time:
> 
>     my @new = grep !exists $hash{$_}, @a;
> 
> Note that the values associated with each key in the hash are irrelevant
> here.
> 
> In a smilar fashion hashes can be used to filter out duplicates or
> calculate the intersection of two lists (in which case you'd just have
> to negate the grep-condition in the above statement).
>     
> >  I finally settled for the old C way of doing it of having a loop in a
> > loop to compare each value of one array against each value of the
> > other.
> 
> You can do that, sure, but it becomes impractical for large lists.
> 
> Tassilo


Thanks!  I guess I needed to sleep on it and let it sink in.  I woke
up this morning and my first thought was "the key is the key! you just
have to ask if it exists or not."  I ran to my computer to look at
everyones examples to see if I was right.  I found your beautiful
explanation waiting for me.  Maybe what I am asking for is more in
line with the PHP philosophy, which is to provide a simple built in
function for everything so people don't have to think too much.  Now I
am glad that there was no built in function so I had a chance to learn
something new.

wana


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

Date: 2 Oct 2004 07:53:52 -0700
From: ioneabu@yahoo.com (wana)
Subject: Re: making easy things difficult: @a-@b
Message-Id: <bf0b47ca.0410020653.22500027@posting.google.com>

> //  I finally settled for the old C way of doing it of having a loop in a
> //  loop to compare each value of one array against each value of the
> //  other.
> 
> That's very inefficient. If both arrays contain 1000 elements, you
> would be doing 1_000_000 comparisons.
> 
Not if you break out of the inner loop when you find a match.  This
will cut back considerably on the number of comparisons.


> The solutions from the faq are (nearly) linear.
> 

Yes, after you have the hash created.  How long does it take to index
a newly created hash so you can look up those keys so quickly?


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

Date: 02 Oct 2004 16:58:23 GMT
From: abuse@dopiaza.cabal.org.uk (Peter Corlett)
Subject: Re: making easy things difficult: @a-@b
Message-Id: <415ede2f$0$94921$5a6aecb4@news.aaisp.net.uk>

wana <ioneabu@yahoo.com> wrote:
[...]
> Not if you break out of the inner loop when you find a match. This
> will cut back considerably on the number of comparisons.

It won't cut back any comparisons if there is no match, and even if
there is a match, it's still highly inefficient.

I think you need to read up on algorithmic complexity and big-O
notation to know why such a linear search approach is bad.

-- 
There are three reasons for becoming a writer: the first is that you need the
money; the second that you have something to say that you think the world
should know; the third is that you can't think what to do with the long winter
evenings.							- Quentin Crisp


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

Date: 2 Oct 2004 10:06:17 -0700
From: topmind@technologist.com (Topmind)
Subject: Re: modeling an object many to many relationship with a transactional bridge table
Message-Id: <4e705869.0410020906.3bb2bb0e@posting.google.com>

> I'll use the standard example, since it's actually quite literally the
> case that I have now. I have students and classes. Students have many
> classes, classes have many students. It's in my database in the
> standard way, I have a students table and a classes table, and a
> student_class_rlt table to join the two of them with my many-to-many.
> 
> But, of course, that's only how my data is stored and I really care
> about how its accessed, and that's all through objects sitting on top
> of a persistence layer.
> 
> Ideally, I'd like to be able to access my classes from a student via a
> method:
> 
> my @classes = $students->classes;
> 
> And I can do that by having the persistence layer map the
> student_class_rlt table to a nonsense class ("StudentClassRLT") and
> then populating those objects Class attribute and returning it in
> their place. Standard stuff.
> 
> The issue, as always, is that my join table contains additional
> information, start_date and end_date, for instance (when the student
> entered and exited that class), so I can't simply throw away that
> middle object.

This sounds like suspicious normalization. Perhaps I am
misinterpreting it. The course 
date range should be stored with the course, not with the
many-to-many table. It is not different per student.
I suppose if a student drops out
of a class we may need to store the drop-out date.
However, that may be more info than the system needs.
A "teacher's note" column may be sufficient for that.

You may be interested in this little campus schema 
experiment:

http://www.c2.com/cgi/wiki?CampusExample

As far as OO design, I don't believe in OO modeling for
such domains, so I won't comment on them.

-T-


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

Date: Sat, 02 Oct 2004 10:43:24 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: replace string in file
Message-Id: <gDv7d.188248$3l3.147851@attbi_s03>

Paul Lalli wrote:

> This is such a common use for perl that there's a couple command line
> arguments available to make this a one-liner:
> 
> perl -pe"s/EXEC/arg0/; s/NAME/arg1/;" ./template.desktop >$HOME/Desktop/arg2

The /g modifier is still required; "EXEC" could appear multiple times on one 
line as could "NAME".
	-Joe


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

Date: Sat, 02 Oct 2004 04:14:53 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <1Xp7d.539$x65.53@trnddc06>

Edward Wijaya wrote:
> With this hashes:
>
> $hash = {
>           '1-1' => 3,
>           '2-3' => 2,
>           '2-2' => 1,
>           '1-2' => 6,
>           '1-3' => 3
>         };
>
> What's the best to retrieve the top-N hashes
> from it. So, with N = 3 it will become:

Hashes by definition don't have a (usable) sequence or order. Therefore your 
request for the top n elements of a hash doesn't make any sense.

If you want/need to preserve the sequence of elements then you picked the 
wrong data structure.
Use an array instead. And to get the first $n elements use an array slice
    @foo = @bar[0..$n-1];

jue 




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

Date: Sat, 02 Oct 2004 13:56:26 +0800
From: "Edward Wijaya" <ewijaya@singnet.com.sg.removethis>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <opse8ascs1avcff0@news.singnet.com.sg>

On Sat, 02 Oct 2004 04:14:53 GMT, Jürgen Exner <jurgenex@hotmail.com>  
wrote:

Hi,
Thanks so much for the reply:

> Hashes by definition don't have a (usable) sequence or order. Therefore  
> your request for the top n elements of a hash doesn't make any sense.

Sorry for not being clear before.
I should have sorted the hash according to it's value
and then pick top-N from it.

> Use an array instead.
It's difficult for me to change the current
data-structure of my code, as it's inherently designed
for hash.

Moreover, I found about Tie::IxHash module.
Which is suppose to "impose" order in the hash.
However since I am new in using Method through external
module, there are 2 issues I fail to deal with.
Your valuable suggestion is important to me.

Here is my code:

__BEGIN__
use strict;
use warnings;
use Tie::IxHash;

my $N = 2;
my %hash = {
      #1) Must sort by value,then 2) Select Top N after sorting it.
            '1-1' => 3,
            '2-3' => 2,
            '2-2' => 1,
            '1-2' => 6,
            '1-3' => 3
          };

my $t = Tie::IxHash->new(%hash);
$t->SortByValue;                #sorting it, is this correct?
my  @topN = $t->Splice(0, $N);  #splicing it

__END__

My problem is:
1. Is the way I invoke the Sorted new hash is correct?,
    How can I view it? I tried: print "%$hash\n"; # doesn't seem to work
2. The "spliced" array is in the form of array,
    Is there any efficient way/method to recover it into Hash?

Thanks and hope to hear from you again,

Regards
Edward WIJAYA
SINGAPORE


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

Date: Sat, 02 Oct 2004 14:12:42 +0800
From: "Edward Wijaya" <ewijaya@singnet.com.sg.removethis>
To: =?iso-8859-1?Q?J=FCrgen_Exner?= <jurgenex@hotmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <opse8bjgeyavcff0@news.singnet.com.sg>



Sorry.
Slight correction, the hash should come with bracket:

my %hash =(
             #1) Must sort by value,then 2) Select Top N after sorting it.
            '1-1' => 3,
            '2-3' => 2,
            '2-2' => 1,
            '1-2' => 6,
            '1-3' => 3
          );


Regards,
Edward WIJAYA
SINGAPORE


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

Date: Sat, 02 Oct 2004 06:19:06 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <uLr7d.452$pw4.108@trnddc01>

Edward Wijaya wrote:
> On Sat, 02 Oct 2004 04:14:53 GMT, Jürgen Exner <jurgenex@hotmail.com>
> wrote:
>> Hashes by definition don't have a (usable) sequence or order.
>> Therefore your request for the top n elements of a hash doesn't make
>> any sense.
>
> Sorry for not being clear before.
> I should have sorted the hash according to it's value
> and then pick top-N from it.

Again, a hash does not have a (usable) sequence, therefore the very notion 
of sorting a hash doesn't make sense.

You may sort a list of the keys of the hash (sort keys %myhash) or a list of 
the values of the hash (sort values %myhash), but you cannot sort a hash.

>> Use an array instead.
> It's difficult for me to change the current
> data-structure of my code, as it's inherently designed
> for hash.
>
> Moreover, I found about Tie::IxHash module.

Sorry, never used that one.

jue 




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

Date: Sat, 02 Oct 2004 10:14:15 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <Xbv7d.3193$N%.547@edtnps84>

Jürgen Exner wrote:
> Edward Wijaya wrote:
>>
>>Sorry for not being clear before.
>>I should have sorted the hash according to it's value
>>and then pick top-N from it.
> 
> Again, a hash does not have a (usable) sequence, therefore the very notion 
> of sorting a hash doesn't make sense.
> 
> You may sort a list of the keys of the hash (sort keys %myhash) or a list of 
> the values of the hash (sort values %myhash), but you cannot sort a hash.

Sure you can. A hash returns a list like any other list.

$ perl -le' %x = qw/ a b c d x y /; print for %x; print; print for sort %x'
c
d
a
b
x
y

a
b
c
d
x
y



John
-- 
use Perl;
program
fulfillment


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

Date: Sat, 02 Oct 2004 11:36:22 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <Wow7d.155045$MQ5.13165@attbi_s52>

Edward Wijaya wrote:

> On Sat, 02 Oct 2004 04:14:53 GMT, Jürgen Exner <jurgenex@hotmail.com>  
> wrote:
>> Hashes by definition don't have a (usable) sequence or order. 
>> Therefore  your request for the top n elements of a hash doesn't make 
>> any sense.
> 
> Sorry for not being clear before.
> I should have sorted the hash according to it's value
> and then pick top-N from it.

You cannot sort a hash.  Period.

You can sort the hash keys into a list and then use that list to
access the corresponding values in order by key, but the hash
itself remains unsorted.

   @sorted_keys = sort keys %hash;
   print "$_ -> $hash{$_}\n" for @sorted_keys;
   print "keys:   @sorted_keys\n";
   print "values: @hash{@sorted_keys}\n";

Or you can create an array of keys sorted such that the corresponding
values are in order.

   $N = $number_of_items_to_extract;
   @keys_sorted_by_value = sort { $hash{$a} cmp $hash{$b} } keys %hash;
   @top_N_keys = splice @keys_sorted_by_value,-$N;
   print "The keys @top_N_keys have values @hash{@top_N_keys}\n";
   print "The remaining values are @hash{@keys_sorted_by_value}\n";

>> Use an array instead.
> 
> It's difficult for me to change the current
> data-structure of my code, as it's inherently designed
> for hash.

In that case, use a hash and an array, as shown above.
	-Joe


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

Date: Sat, 02 Oct 2004 11:43:52 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <Yvw7d.1433$Xk1.1237@trnddc02>

John W. Krahn wrote:
> Jürgen Exner wrote:
>> Edward Wijaya wrote:
>>>
>>> Sorry for not being clear before.
>>> I should have sorted the hash according to it's value
>>> and then pick top-N from it.
>>
>> Again, a hash does not have a (usable) sequence, therefore the very
>> notion of sorting a hash doesn't make sense.
>>
>> You may sort a list of the keys of the hash (sort keys %myhash) or a
>> list of the values of the hash (sort values %myhash), but you cannot
>> sort a hash.
>
> Sure you can. A hash returns a list like any other list.
>
> $ perl -le' %x = qw/ a b c d x y /; print for %x; print; print for
> sort %x' c

Ok, I never thought of it that way!
Good that I set my coffee down before reading your post, otherwise I would 
have to clean my keyboard now....

This is really a good one, should keep it in mind for the next person asking 
how to sort a hash.

jue 




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

Date: Sat, 02 Oct 2004 07:40:14 -0400
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <Brw7d.27177$MD5.1436815@news20.bellglobal.com>

John W. Krahn wrote:
> Sure you can. A hash returns a list like any other list.
> 
> $ perl -le' %x = qw/ a b c d x y /; print for %x; print; print for sort %x'

No, these convert the hash to an array, then sorts it.

	--- Shawn


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

Date: Sat, 2 Oct 2004 09:59:35 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Return top-N of Hashes - hash splice?
Message-Id: <slrncltgin.1pt.tadmc@magna.augustmail.com>

Shawn Corey <shawn.corey@sympatico.ca> wrote:
> John W. Krahn wrote:
>> Sure you can. A hash returns a list like any other list.
>> 
>> $ perl -le' %x = qw/ a b c d x y /; print for %x; print; print for sort %x'
> 
> No, these convert the hash to an array,
                             ^^^^^^^^^^^

No it doesn't. 

There is no array being used anywhere in that code.

See this Perl FAQ:

   What is the difference between a list and an array?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Sat, 02 Oct 2004 10:56:31 GMT
From: Joe Smith <Joe.Smith@inwap.com>
Subject: Re: Scripting trouble
Message-Id: <zPv7d.55734$He1.41427@attbi_s01>

y wrote:

> bash: /Users/Y/script: /usr/bin/perl: bad interpreter: Permission denied

Try running
	/usr/bin/perl -v
from the bash prompt.  If it says Permission denied, there's your culprit.
		-Joe


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

Date: Sat, 02 Oct 2004 09:09:19 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Syntax appears inconsistent - why is this?
Message-Id: <3fu7d.3191$N%.2983@edtnps84>

Abigail wrote:
> Joe Smith (Joe.Smith@inwap.com) wrote on MMMMXLVIII September MCMXCIII in
> <URL:news:TeS6d.146050$D%.55455@attbi_s51>:
> %%  
> %%  I would phrase it differently.
> %%     ( ) are used to build lists, which can populate arrays and hashes.
> 
> Eh, wrong. () seldomly build lists. It certainly doesn't in:
> 
>     my %hash = (key1 => 'val1', key2 => 'val2');
> 
> The parens in the above expression play exactly the same role as
> they do in:
> 
>     my $val = 3 * (4 + 5);
> 
> They help the parser to construct a parse-tree.

Could you (or anyone) explain what the inner parentheses are doing in the 
second example and why please?

while  ()  { print "infinite loop\n" }

while (()) { print "will not print\n" }


:-)

John
-- 
use Perl;
program
fulfillment


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

Date: Sat, 02 Oct 2004 15:29:17 GMT
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Re: Syntax appears inconsistent - why is this?
Message-Id: <hPz7d.25159$kq6.14518288@news4.srv.hcvlny.cv.net>

Abigail wrote:
> Eh, wrong. () seldomly build lists. It certainly doesn't in:

>     my %hash = (key1 => 'val1', key2 => 'val2');

> The parens in the above expression play exactly the same role as
> they do in:
> 
>     my $val = 3 * (4 + 5);

No, the () /are/ building a list above.  The statement is syntactic 
sugar for:

       my %hash = ('key1', 'val1', 'key2', 'val2');

-- 
John W. Kennedy
"The poor have sometimes objected to being governed badly; the rich have 
always objected to being governed at all."
   -- G. K. Chesterton.  "The Man Who Was Thursday"


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

Date: Sat, 2 Oct 2004 11:06:10 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: While query
Message-Id: <Pine.SOL.3.96.1041002110541.3684A-100000@vcmr-86.server.rpi.edu>

On 1 Oct 2004, A. Sinan Unur wrote:

>"P.R.Brady" <iss025@bangor.ac.uk> wrote in
>news:415D9051.60300@bangor.ac.uk: 
>
>> So to empty and process my array/stack I need to:
>> while (scalar(@array)) {
>>      $_=pop(@array);
>>          ...
>> }
>
>You could do 
>
>use strict;
>use warnings;
>
>my @array = ('hello', '');
>
>while(@array and my ($x) = pop @array) {
>    print "Popped: [$x]\n";
>}

No, he couldn't.  It *still* breaks out of the while conditional when
pop() returns a false element.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
  Senior Dean, Fall 2004    %  have long ago been overpaid?
RPI Corporation Secretary   %
http://japhy.perlmonk.org/  %    -- Meister Eckhart




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

Date: Sat, 2 Oct 2004 11:07:16 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: While query
Message-Id: <Pine.SOL.3.96.1041002110656.3684C-100000@vcmr-86.server.rpi.edu>

On 1 Oct 2004, A. Sinan Unur wrote:

>"P.R.Brady" <iss025@bangor.ac.uk> wrote in
>news:415D9051.60300@bangor.ac.uk: 
>
>> So to empty and process my array/stack I need to:
>> while (scalar(@array)) {
>>      $_=pop(@array);
>>          ...
>> }
>
>You could do 
>
>use strict;
>use warnings;
>
>my @array = ('hello', '');
>
>while(@array and my ($x) = pop @array) {
>    print "Popped: [$x]\n";
>}

Sorry, I jumped the gun.  I didn't notice the parentheses around $x.

Nice trick.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
  Senior Dean, Fall 2004    %  have long ago been overpaid?
RPI Corporation Secretary   %
http://japhy.perlmonk.org/  %    -- Meister Eckhart




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

Date: 2 Oct 2004 15:14:40 GMT
From: "A. Sinan Unur" <usa1@llenroc.ude.invalid>
Subject: Re: While query
Message-Id: <Xns9576729AC30F3asu1cornelledu@132.236.56.8>

Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote in 
news:Pine.SOL.3.96.1041002110656.3684C-100000@vcmr-86.server.rpi.edu:

> On 1 Oct 2004, A. Sinan Unur wrote:
> 
>>use strict;
>>use warnings;
>>
>>my @array = ('hello', '');
>>
>>while(@array and my ($x) = pop @array) {
>>    print "Popped: [$x]\n";
>>}
> 
> Sorry, I jumped the gun.  I didn't notice the parentheses around $x.

Don't worry about it. As Abigail also noted, I should have explained why & 
how and pointed out the necessity of the paranthesses around the $x. 
Otherwise, it is easy to miss them.

> Nice trick.

Thanks.

Sinan.



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

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 7201
***************************************


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