[25932] in Perl-Users-Digest
Perl-Users Digest, Issue: 8152 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jun 6 14:05:31 2005
Date: Mon, 6 Jun 2005 11: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 Mon, 6 Jun 2005 Volume: 10 Number: 8152
Today's topics:
Re: Anybody know a script or method to achieve this? (a <glex_no-spam@qwest-spam-no.invalid>
Re: counting word occurances (Anno Siegel)
Re: counting word occurances <1usa@llenroc.ude.invalid>
Re: counting word occurances (Anno Siegel)
Re: counting word occurances <vticau@excite.com>
Re: counting word occurances <1usa@llenroc.ude.invalid>
eval not work <ngoc@yahoo.com>
Re: eval not work <1usa@llenroc.ude.invalid>
Multilanguage capable scripts howto? <krienke@uni-koblenz.de>
Re: Multilanguage capable scripts howto? <1usa@llenroc.ude.invalid>
Re: Multilanguage capable scripts howto? <1usa@llenroc.ude.invalid>
Newbie question: how to browse for a file in Tk? <amheiserbush@yahoo.com.au>
Re: Newbie question: how to browse for a file in Tk? (Anno Siegel)
Re: Newbie question: how to browse for a file in Tk? <amheiserbush@yahoo.com.au>
Re: Newbie question: how to browse for a file in Tk? <notvalid@email.com>
random numbers <h0p2468@yahoo.com>
Re: random numbers <1usa@llenroc.ude.invalid>
Re: random numbers <pilkowsk@informatik.uni-marburg.de>
Re: random numbers xhoster@gmail.com
Re: random numbers <ThomasKratz@REMOVEwebCAPS.de>
Re: random numbers <h0p2468@yahoo.com>
Re: Randomly selecting Variables <sbryce@scottbryce.com>
Re: Randomly selecting Variables (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 06 Jun 2005 12:42:17 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Anybody know a script or method to achieve this? (authentication/access related)
Message-Id: <_V%oe.23$ce4.1197@news.uswest.net>
Jim J wrote:
> I have been trying to find some kind of authentication method or
> script (PHP/perl/javascript or other) to achieve the following, with
> no luck.
>
> Say there are 100 files at a site. A person can choose which file to
> download from a list, and to do so has to enter a code or password.
> When a valid code or password is used they are redirected to a page
> with a direct link to download the file.
>
> Once that code or password has been used to download that one file
> (and the same code/password can be used to access any one of the 100
> files), it is then expired/deleted/made invalid and can't be used
> again to download any other file.
>
> Does anybody know a method or script somewhere that can do this?
This might get you started or give you some ideas:
http://www.stonehenge.com/merlyn/WebTechniques/col68.html
------------------------------
Date: 6 Jun 2005 15:58:53 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: counting word occurances
Message-Id: <d81rrt$pst$1@mamenchi.zrz.TU-Berlin.DE>
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote in comp.lang.perl.misc:
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in
> news:V3Yne.1300$mb2.1255@trnddc07:
> D:\Home> st
> Rate capture strip
> capture 29936/s -- -2%
> strip 30640/s 2% --
>
> OK, you have a point there (and I knew it even before I ran the
> benchmark.
No. 2% difference in a benchmark doesn't constitute a point. Another
benchmark could have them the other way 'round. Results differ much
more than that across compilers and/or machines. On mine, benchmark
has "capture" in favor by 12%.
Anno
------------------------------
Date: Mon, 06 Jun 2005 16:22:16 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966D7D9DC90A5asu1cornelledu@127.0.0.1>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in news:d81rrt$pst
$1@mamenchi.zrz.TU-Berlin.DE:
> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote in comp.lang.perl.misc:
>> "Jürgen Exner" <jurgenex@hotmail.com> wrote in
>> news:V3Yne.1300$mb2.1255@trnddc07:
>
>> D:\Home> st
>> Rate capture strip
>> capture 29936/s -- -2%
>> strip 30640/s 2% --
>>
>> OK, you have a point there (and I knew it even before I ran the
>> benchmark.
>
> No. 2% difference in a benchmark doesn't constitute a point.
> Another benchmark could have them the other way 'round.
> Results differ much more than that across compilers and/or machines.
> On mine, benchmark has "capture" in favor by 12%.
Indeed. Two hasty posts on my part in the same thread.
asu1@recex:/home/asu1/.tmp > perl -v
This is perl, v5.8.6 built for i386-freebsd-64int
asu1@recex:/home/asu1/.tmp > perl ttt.pl
Rate strip capture
strip 20283/s -- -13%
capture 23272/s 15% --
Thanks for pointing this out.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 6 Jun 2005 16:22:12 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: counting word occurances
Message-Id: <d81t7k$pst$2@mamenchi.zrz.TU-Berlin.DE>
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote in comp.lang.perl.misc:
> vali <vticau@excite.com> wrote in
> news:r80oe.6452$II3.3529@news.cpqcorp.net:
>
> > Jürgen Exner wrote:
>
> >> See
> >> perldoc -q "strip blank"
> >>
> >> Another difference between our solutions would be the handling of
> >> lines that contain more than one single word, e.g. "green grapes" or
> >> "mini-tomatos". Which behaviour the OP wants is everybody's guess.
>
> ...
>
> > Wasn't aware about the above faq. I've been using for years:
> > s/(^\s+|\s+$)//g;
> > which seems to be the same (or not ?!) as:
> > s/^\s*//; s/\s*$//;
>
>
> Not functionally the same. Your expression requires at least one \s either
> at the beginning or the end.
>
> Second, it uses alternation in the regex which is generally more expensive.
>
> Third, you are unnecessarily capturing.
Fourth, the alternation keeps the second part from matching right away
when there are both leading and trailing blanks. The regex must be
applied again (via /g), so that its first alternative can fail, and the
second one can match the trailing blanks.
A pattern that starts with "^" and ends with "$" shouldn't normally
need /g.
Anno
------------------------------
Date: Mon, 06 Jun 2005 17:10:55 GMT
From: vali <vticau@excite.com>
Subject: Re: counting word occurances
Message-Id: <zs%oe.6528$7h7.4736@news.cpqcorp.net>
A. Sinan Unur wrote:
> "John W. Krahn" <someone@example.com> wrote in
> news:HX2oe.31147$on1.29725@clgrps13:
>
>
>>A. Sinan Unur wrote:
>>
>>>vali <vticau@excite.com> wrote in
>>>news:r80oe.6452$II3.3529@news.cpqcorp.net:
>
>
> ...
>
>
>>>>Wasn't aware about the above faq. I've been using for years:
>>>>s/(^\s+|\s+$)//g;
>>>>which seems to be the same (or not ?!) as:
>>>>s/^\s*//; s/\s*$//;
>
>
> ...
>
>>>sub faq {
>>> my @input = @{ $INPUT };
>>> for (@input) {
>>> s/^\s*//;
>>> s/\s*$//;
>>> }
>>>}
>>
>>The FAQ uses \s+ instead of \s* which is more efficient.
>
>
> Thank you for the correction. I failed to notice the typo in vali's post.
Just followed up on Jurgen's one.
>
> I was suprised to see just how much more efficient \s+ was compared to \s*.
That's what I thought when using it on my approach.
>
> Finally, this supports my assertion that s/(^\s+|\s+$)//g; is not the same
> as what the answer to the FAQ recommends.
That's right. I'll start using it (and check more often the FAQs).
Thanks (to all of you) for this (and other) insights !
__Vali
>
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> use Benchmark ':all';
>
> my $INPUT = [
> 'pear ',
> ' apple ',
> 'apple',
> ' orange ',
> ' mango ',
> 'mango',
> ' pear',
> ' cherry ',
> 'apple',
> '',
> ];
>
> sub s1 {
> my @input = @{ $INPUT };
>
> for (@input) {
> s/^\s*//;
> s/\s*$//;
> }
> }
>
> sub faq {
> my @input = @{ $INPUT };
>
> for (@input) {
> s/^\s+//;
> s/\s+$//;
> }
> }
>
> cmpthese 0, {
> s1 => \&s1,
> faq => \&faq,
> };
>
> __END__
>
> D:\Home>perl t.pl
> Rate s1 faq
> s1 8638/s -- -25%
> faq 11489/s 33% --
>
> Sinan
------------------------------
Date: Mon, 06 Jun 2005 17:28:12 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: counting word occurances
Message-Id: <Xns966D88CB5252Aasu1cornelledu@127.0.0.1>
vali <vticau@excite.com> wrote in
news:zs%oe.6528$7h7.4736@news.cpqcorp.net:
[ Please quote properly. You inserted your responses in the middle of
my responses, and did not delete parts you were not replying to. This
makes it difficult to carry on an intelligent conversation with you.
]
> A. Sinan Unur wrote:
>> "John W. Krahn" <someone@example.com> wrote in
>> news:HX2oe.31147$on1.29725@clgrps13:
>>
>>
>>>A. Sinan Unur wrote:
>>>
>>>>vali <vticau@excite.com> wrote in
>>>>news:r80oe.6452$II3.3529@news.cpqcorp.net:
>>
>> ...
>>
>>>>>Wasn't aware about the above faq. I've been using for years:
>>>>>s/(^\s+|\s+$)//g;
>>>>>which seems to be the same (or not ?!) as:
>>>>>s/^\s*//; s/\s*$//;
>>
>> ...
>>
>>>>sub faq {
>>>> my @input = @{ $INPUT };
>>>> for (@input) {
>>>> s/^\s*//;
>>>> s/\s*$//;
>>>> }
>>>>}
>>>
>>>The FAQ uses \s+ instead of \s* which is more efficient.
>>
>> Thank you for the correction. I failed to notice the typo in vali's
>> post.
>
> Just followed up on Jurgen's one.
So what?
>> I was suprised to see just how much more efficient \s+ was compared
>> to \s*.
>
> That's what I thought when using it on my approach.
Is that why you captured unnecessarily, alternated unnecessarily, used g
unnecessarily etc???
>> Finally, this supports my assertion that s/(^\s+|\s+$)//g; is not the
>> same as what the answer to the FAQ recommends.
>
> That's right. I'll start using it (and check more often the FAQs).
You'll start using what?
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 06 Jun 2005 16:17:29 +0200
From: ngoc <ngoc@yahoo.com>
Subject: eval not work
Message-Id: <42a45b60$1@news.broadpark.no>
Hi
My eval do not work. I do not know why?
eval {
system "rename _new.xml .xml *.xml";
};
die 'Can not change new to current data' if $@;
when no xml file exists. It should come to exception situation and print
out 'Can not change new to current data' and exit program. But it
print out the message 'No such file and directory' and go ahead to next
step.
Thanks
ngoc
------------------------------
Date: Mon, 06 Jun 2005 14:24:04 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: eval not work
Message-Id: <Xns966D6993C7DBAasu1cornelledu@127.0.0.1>
ngoc <ngoc@yahoo.com> wrote in news:42a45b60$1@news.broadpark.no:
> My eval do not work. I do not know why?
> eval {
> system "rename _new.xml .xml *.xml";
> };
> die 'Can not change new to current data' if $@;
>
> when no xml file exists. It should come to exception situation and print
> out 'Can not change new to current data' and exit program. But it
> print out the message 'No such file and directory' and go ahead to next
> step.
You are jumping to conclusions. eval works perfectly.
You just failed to read the documentation for system
perldoc -f system
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 06 Jun 2005 13:03:13 +0200
From: Rainer Krienke <krienke@uni-koblenz.de>
Subject: Multilanguage capable scripts howto?
Message-Id: <d81ahh$cll$1@news.uni-koblenz.de>
Hello,
I am looking for a way to make a perl script running on a linux system
output messages in several languages depending on the LOCALE in the
environment. The translations for the output of the script should be kept
in seperate files so the application is seperate from the translation.
Well most of the linux applications I know can do this so I guess there is a
generic i18n API to do this. The problem is I do not find information
exactly how it can be done? Can anyone give me a hint?
Thanks
Rainer
------------------------------
Date: Mon, 6 Jun 2005 15:55:39 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Multilanguage capable scripts howto?
Message-Id: <Xns966D7955C423Dasu1cornelledu@132.236.56.8>
Rainer Krienke <krienke@uni-koblenz.de> wrote in
news:d81ahh$cll$1@news.uni-koblenz.de:
> Hello,
>
> I am looking for a way to make a perl script running on a linux system
> output messages in several languages depending on the LOCALE in the
> environment. The translations for the output of the script should be
> kept in seperate files so the application is seperate from the
> translation.
>
> Well most of the linux applications I know can do this so I guess
> there is a generic i18n API to do this. The problem is I do not find
> information exactly how it can be done? Can anyone give me a hint?
CPAN is your friend.
I like Locale::Maketext:
<URL: http://search.cpan.org/~nwclark/perl-5.8.7/lib/Locale/Maketext.pod>
There are also modules corresponding to gettext etc.
Sinan
------------------------------
Date: Mon, 6 Jun 2005 15:57:22 +0000 (UTC)
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Multilanguage capable scripts howto?
Message-Id: <Xns966D79A069BFAasu1cornelledu@132.236.56.8>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
news:Xns966D7955C423Dasu1cornelledu@132.236.56.8:
> Rainer Krienke <krienke@uni-koblenz.de> wrote in
> news:d81ahh$cll$1@news.uni-koblenz.de:
...
>> Well most of the linux applications I know can do this so I guess
>> there is a generic i18n API to do this. The problem is I do not find
>> information exactly how it can be done? Can anyone give me a hint?
...
> I like Locale::Maketext:
>
> <URL:
> http://search.cpan.org/~nwclark/perl-5.8.7/lib/Locale/Maketext.pod>
Actually, you probably want:
<URL: http://search.cpan.org/~sburke/Locale-Maketext-1.09/>
Sinan
------------------------------
Date: Mon, 6 Jun 2005 11:16:39 -0400
From: "James Calivar" <amheiserbush@yahoo.com.au>
Subject: Newbie question: how to browse for a file in Tk?
Message-Id: <d81pco$o6h$1@home.itg.ti.com>
Hello all,
I'm taking my first stab at writing a Tk interface for a Perl script that I
have developed. The existing Perl script reads configuration parameters
from a separate .ini file, and uses those values to do its thing while
running. Some of these config params are pathnames and filenames on the
system that the script is running on. What I'd like to be able to do is to
have the user be able to browse their filesystem using the mouse, and select
items that way. However, in looking through "Learning Perl/Tk," I didn't
immediately see anything that would allow me to do it, so I'm wondering if
someone here could give me an idea on how such a task is accomplished in
Perl/Tk.
Incidentally, this script is running on a Win32 platform (XP Pro) and I've
got ActiveState 5.8.4 installed.
Thanks for any help,
James
------------------------------
Date: 6 Jun 2005 15:30:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Newbie question: how to browse for a file in Tk?
Message-Id: <d81q6a$o60$2@mamenchi.zrz.TU-Berlin.DE>
James Calivar <amheiserbush@yahoo.com.au> wrote in comp.lang.perl.misc:
[...]
> system that the script is running on. What I'd like to be able to do is to
> have the user be able to browse their filesystem using the mouse, and select
> items that way.
Do a CPAN search for the keywords "Tk" and "file".
Anno
------------------------------
Date: Mon, 6 Jun 2005 13:26:15 -0400
From: "James Calivar" <amheiserbush@yahoo.com.au>
Subject: Re: Newbie question: how to browse for a file in Tk?
Message-Id: <d820vq$str$1@home.itg.ti.com>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:d81q6a$o60$2@mamenchi.zrz.TU-Berlin.DE...
> James Calivar <amheiserbush@yahoo.com.au> wrote in comp.lang.perl.misc:
>
> [...]
>
> > system that the script is running on. What I'd like to be able to do is
to
> > have the user be able to browse their filesystem using the mouse, and
select
> > items that way.
>
> Do a CPAN search for the keywords "Tk" and "file".
>
> Anno
Anno,
Thanks for the tip. Alright - I've used ppm to download a module called
"Tk-FileDialog". But when I try to run the test script located at
http://cpan.uwinnipeg.ca/htdocs/Tk-FileDialog/Tk/FileDialog.html, I get the
following:
c:\>perl test.pl
can't make ".filedialog" its own master at C:/Perl/site/lib/Tk/Submethods.pm
line 37.
at C:/Perl/site/lib/Tk/FileDialog.pm line 470
Any idea what's wrong?
Thanks,
James
------------------------------
Date: Mon, 06 Jun 2005 18:00:01 GMT
From: Ala Qumsieh <notvalid@email.com>
Subject: Re: Newbie question: how to browse for a file in Tk?
Message-Id: <Ba0pe.581$Z44.347@newssvr13.news.prodigy.com>
James Calivar wrote:
> Hello all,
>
> I'm taking my first stab at writing a Tk interface for a Perl script that I
> have developed. The existing Perl script reads configuration parameters
> from a separate .ini file, and uses those values to do its thing while
> running. Some of these config params are pathnames and filenames on the
> system that the script is running on. What I'd like to be able to do is to
> have the user be able to browse their filesystem using the mouse, and select
> items that way. However, in looking through "Learning Perl/Tk," I didn't
> immediately see anything that would allow me to do it, so I'm wondering if
> someone here could give me an idea on how such a task is accomplished in
> Perl/Tk.
You need to use getOpenFile:
$top->getOpenFile(options);
check out the docs for Tk::getOpenFile for more info.
--Ala
------------------------------
Date: Mon, 6 Jun 2005 20:18:29 +0800
From: "Mei" <h0p2468@yahoo.com>
Subject: random numbers
Message-Id: <d81eva$h2i$1@news.isu.edu.tw>
Hi,
I want to generate 6 random numbers from 1 to 10.
The 6 numbers must not the same.
These numbers must match to the number of an array.
I like to know how many iterations to get the match.
I am having troubles with the do-until loop. Could someone point out the
problem for me?
#!/usr/bin/perl -w
# generate 6 random numbers (1~10) that match exactly to the @ticket
# count the number of loops
use strict;
srand(time||$$);
my @ticket = sort (2, 3, 5, 7, 8, 10);
my @all_number = ();
my @new_number = ();
my $count = 0;
# generate 10 numbers
for (my $i = 0; $i < 10; $i++) {
my $number = $i + 1;
push (@all_number, $number)
}
do {
#reset @new_number to the empty array each time through the loop.
@new_number = ();
#get 6 random numbers
for (my $i = 0; $i < 6; $i++) {
my $position = randomposition (@all_number);
# Pick a random number from @all_number
my $number = splice (@all_number, $position, 1);
push (@new_number, $number);
}
@new_number = sort (@new_number);
print "@new_number\n";
$count++;
} until (($new_number[0] eq "$ticket[0]") && ($new_number[1] eq
"$ticket[1]")
&& ($new_number[2] eq "$ticket[2]") && ($new_number[3] eq
"$ticket[3]")
&& ($new_number[4] eq "$ticket[4]") && ($new_number[5] eq
"$ticket[5]"));
print $count, "\n";
##########################################################
sub randomposition {
my(@all_number) = @_;
# This expression returns a random number
return int(rand(scalar(@all_number)));
}
------------------------------
Date: Mon, 06 Jun 2005 13:52:05 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: random numbers
Message-Id: <Xns966D64273A473asu1cornelledu@127.0.0.1>
"Mei" <h0p2468@yahoo.com> wrote in news:d81eva$h2i$1@news.isu.edu.tw:
[ what's up with the extra line-feeds? ]
> I want to generate 6 random numbers from 1 to 10.
> The 6 numbers must not the same.
> These numbers must match to the number of an array.
> I like to know how many iterations to get the match.
I am not sure what you mean by this.
> #!/usr/bin/perl
>
> use strict;
use warnings;
is better because it allows you to turn warnings for selected lexical
scopes.
> srand(time||$$);
Why? Note that, unless your Perl is severely broken, this is equivalent
to srand(time). From perldoc -f srand:
In versions of Perl prior to 5.004 the default seed was just the
current "time". This isn't a particularly good seed, so many old
programs supply their own seed value (often "time ^ $$" or "time
^ ($$ + ($$ << 15))"), but that isn't necessary any more.
> my @ticket = sort (2, 3, 5, 7, 8, 10);
> my @all_number = ();
> my @new_number = ();
> my $count = 0;
Declare variables in the smallest applicable scope.
> # generate 10 numbers
>
> for (my $i = 0; $i < 10; $i++) {
>
> my $number = $i + 1;
>
> push (@all_number, $number)
>
> }
So, instead of this for loop, use:
my @all_number = (1 .. 10);
> do {
> #reset @new_number to the empty array each time through the loop.
> @new_number = ();
my @new_number();
>
> #get 6 random numbers
> for (my $i = 0; $i < 6; $i++) {
> my $position = randomposition (@all_number);
> # Pick a random number from @all_number
> my $number = splice (@all_number, $position, 1);
> push (@new_number, $number);
> }
This is realy convoluted (and therefore confuses me still further as to
what you are trying to do).
for (1 .. 6) {
push @new_number, $all_number[ int(rand( scalar @all_number )) ];
}
It seems to me like what you are really doing is to check how long it
would take for you to generate the sequence (0, 1, 2, 3, 4, 5) by
randomly selecting (with replacement) from the set {0, 1, 2, 3, 4, 5}.
Here is one way of doing it:
# !/usr/bin/perl
use strict;
use warnings;
my @ticket = sort {$a <=> $b } (2, 3, 5, 7, 8, 10);
my $ticket = "@ticket";
my $count = 0;
while ( 1 ) {
++$count;
my @new;
for (1 .. 6) {
push @new, $ticket[ int(rand( scalar @ticket )) ];
}
@new = sort {$a <=> $b } @new;
my $new = "@new";
print "$new\tvs\t$ticket\n";
last if $new eq $ticket;
}
print "Count = $count\n";
__END__
Please do read the posting guidelines for this group tolearn how you can
help yourself and help others help you.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 6 Jun 2005 15:53:58 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: random numbers
Message-Id: <3gj2ppFco6lsU1@individual.net>
* Mei schrieb:
>
> I want to generate 6 random numbers from 1 to 10.
> The 6 numbers must not the same.
> These numbers must match to the number of an array.
> I like to know how many iterations to get the match.
>
> I am having troubles with the do-until loop. Could someone point out the
> problem for me?
>
> #!/usr/bin/perl -w
> # generate 6 random numbers (1~10) that match exactly to the @ticket
> # count the number of loops
> use strict;
> srand(time||$$);
You don't have to call srand() explicitly. It is called implicitly at
the first use of rand() since your Perl isn't very old. Have a look at
`perldoc -f srand` for details.
>
> my @ticket = sort (2, 3, 5, 7, 8, 10);
Your numbers are already sorted numerically. After calling sort() on
them, your list looks like (10,2,3,5,7,8). This doesn't matter since you
sort your numbers in the same order later -- but be aware of this.
>
> my @all_number = ();
> my @new_number = ();
> my $count = 0;
>
> # generate 10 numbers
> for (my $i = 0; $i < 10; $i++) {
> my $number = $i + 1;
> push (@all_number, $number)
> }
To get a list with all numbers from 1 to 10, please use
my @all_number = 1 .. 10;
>
> do {
> #reset @new_number to the empty array each time through the loop.
> @new_number = ();
>
> #get 6 random numbers
> for (my $i = 0; $i < 6; $i++) {
> my $position = randomposition (@all_number);
> # Pick a random number from @all_number
> my $number = splice (@all_number, $position, 1);
> push (@new_number, $number);
> }
Consider to use map instead of this for loop. And IMHO is splice() too
much pverhead for this -- you could access each array element directly
since you know its position. Try to use one of (I don't know which is
more readable to you):
@new_number = map { $all_number[ rand @all_number ] } 0 .. 5;
@new_number = @all_number[ map { rand @all_number } 0 .. 5 ];
>
> @new_number = sort (@new_number);
This is what I've meant above. This sort() doesn't sort your numbers
numerically too. But it's just important to sort the numbers in the same
order as above (and this is what you're doing). If -- and only if -- you
left out the sort statement above, you should sort numerically here, I
think your code gets more clarity then.
@new_number = sort { $a <=> $b } @new_number;
>
> print "@new_number\n";
> $count++;
> } until (($new_number[0] eq "$ticket[0]") && ($new_number[1] eq "$ticket[1]")
> && ($new_number[2] eq "$ticket[2]") && ($new_number[3] eq "$ticket[3]")
> && ($new_number[4] eq "$ticket[4]") && ($new_number[5] eq "$ticket[5]"));
To compare numbers there is the == operator, eq is for comparing strings
only. I haven't checked it out yet, but AFAIK the numerical ones should
be a little bit faster. And well, it's not funny to read such repeated
code fragments. Perhaps you could use grep() instead.
} until ! grep { $ticket[$_] != $new[$_] } 0 .. 5;
>
> print $count, "\n";
>
> ##########################################################
>
> sub randomposition {
> my(@all_number) = @_;
> return int(rand(scalar(@all_number)));
> }
This sub could be shortened to
sub randomposition { int rand @_ }
and since writing "int rand @_" is shorter than "randomposition" you
could omit to declare a sub for such a triviality.
All in one, I would write your script as something like:
#!/usr/bin/perl -w
use strict;
my @ticket = sort 2, 3, 5, 7, 8, 10;
my @all = 1 .. 10;
my @new;
my $count = 0;
do {
@new = sort @all[ map { rand @all } 0 .. 5 ];
$count++;
} until ! grep { $ticket[$_] != $new[$_] } 0 .. 5;
print $count;
__END__
regards,
fabian
------------------------------
Date: 06 Jun 2005 15:07:20 GMT
From: xhoster@gmail.com
Subject: Re: random numbers
Message-Id: <20050606110720.442$TS@newsreader.com>
"Mei" <h0p2468@yahoo.com> wrote:
> Hi,
>
> I want to generate 6 random numbers from 1 to 10.
>
> The 6 numbers must not the same.
my %h;
$h{int(rand(10)+1)} =1 while (6 > keys %h) ;
# the keys of %h are now 6 distinct random numbers between 1 and 10.
(I couldn't figure out how your code pertained to what you said you wanted
above, so I ignored it)
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Mon, 06 Jun 2005 17:32:26 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: random numbers
Message-Id: <42a46c8a$0$11221$bb690d87@news.main-rheiner.de>
Fabian Pilkowski wrote:
> * Mei schrieb:
>
>>I want to generate 6 random numbers from 1 to 10.
>>The 6 numbers must not the same.
>>These numbers must match to the number of an array.
>>I like to know how many iterations to get the match.
> All in one, I would write your script as something like:
>
>
> #!/usr/bin/perl -w
> use strict;
>
> my @ticket = sort 2, 3, 5, 7, 8, 10;
> my @all = 1 .. 10;
> my @new;
> my $count = 0;
>
> do {
> @new = sort @all[ map { rand @all } 0 .. 5 ];
> $count++;
> } until ! grep { $ticket[$_] != $new[$_] } 0 .. 5;
>
> print $count;
> __END__
or if only the number of iterations is of interest:
use strict;
use warnings;
my %ticket = map { $_ => 1 } qw/2 3 5 7 8 10/;
my $count = 0;
$count++ && delete $ticket{int(rand(10))+1} while keys %ticket;
print $count;
Thomas
--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
------------------------------
Date: Tue, 7 Jun 2005 00:34:20 +0800
From: "Mei" <h0p2468@yahoo.com>
Subject: Re: random numbers
Message-Id: <d81tuu$1chj$1@news.isu.edu.tw>
Hi,
--Thanks all. The reason that I did not use 1..10 is because the six numbers
must be different.
--Once I take off the do-until loop, you will see the result like: ¡§1 3 4 6
8 10 vs 2 3 5 7 8 10¡¨. So far, this works.
-- But as you pointed out, I want to see how long would it take for me to
generate the sequence (2, 3, 5, 7, 8, 10). So far, I am still having
troubles.
###### modified from the previous script #####
#!/usr/bin/perl -w
# generate 6 random numbers (1~10) that match exactly to the @ticket
# count the number of loops
use strict;
my @ticket = sort {$a <=> $b} (2, 3, 5, 7, 8, 10);
my $ticket = "@ticket";
my @all_number = ();
my @new_number = ();
my $count = 0;
# generate 10 numbers
for (my $i = 0; $i < 10; $i++) {
my $number = $i + 1;
push (@all_number, $number)
}
for (my $i = 0; $i < 6; $i++) {
my $position = int(rand(scalar(@all_number)));
# Pick a random number from @all_number
my $number = splice (@all_number, $position, 1);
push (@new_number, $number);
}
@new_number = sort {$a <=> $b} (@new_number);
my $new_number = "@new_number";
print "$new_number\tvs\t$ticket\n";
$count++;
------------------------------
Date: Mon, 06 Jun 2005 08:55:16 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Randomly selecting Variables
Message-Id: <bb2dnWK3cLxN_jnfRVn-3A@comcast.com>
George wrote:
> Hi,
> I am sorry and not sure if this query is directly related to perl, but
> since I want to do this trick in perl so
>
> I have a 30 variables out of which I need to randomly select 15 and
> then randomly equate them numbers between 01 and 90,
> Thanks
Your problem definition is confusing. What is the significance of having
exactly 30 variables in the pool, if you want to select 15 out of 90
values? Could you use a hash with 30 keys? (But why, if you only want to
use 15 of them?)
If you want to use 15 numbers randomly selected from a pool of 90
possible values, assign the values to 90 elements in an array, shuffle
the array, pull the first 15 values from the shuffled array.
------------------------------
Date: 6 Jun 2005 15:22:48 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Randomly selecting Variables
Message-Id: <d81po8$o60$1@mamenchi.zrz.TU-Berlin.DE>
Scott Bryce <sbryce@scottbryce.com> wrote in comp.lang.perl.misc:
[...]
> If you want to use 15 numbers randomly selected from a pool of 90
> possible values, assign the values to 90 elements in an array, shuffle
> the array, pull the first 15 values from the shuffled array.
If it matters, modify the shuffling algorithm to return after 15 steps.
The "already shuffled" elements usually are a compact sub-array that
is easy to return. No need to shuffle the other 75 that aren't looked
at.
Anno
------------------------------
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 8152
***************************************