[22757] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4978 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 13 09:06:02 2003

Date: Tue, 13 May 2003 06:05:15 -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           Tue, 13 May 2003     Volume: 10 Number: 4978

Today's topics:
    Re: @Array - making it null (Villy Kruse)
    Re: @Array - making it null (Anno Siegel)
        accept() troubles Jens.Toerring@physik.fu-berlin.de
    Re: accept() troubles (Anno Siegel)
    Re: bootstrap <kalinabears@hdc.com.au>
        Convert array into hash element <kopetnik@s-pam-nie.yahoo.com>
    Re: Convert array into hash element <nospam@nospam.de>
    Re: Convert array into hash element (Anno Siegel)
        how to set INC globally <spanda@cisco.com>
    Re: how to set INC globally (Anno Siegel)
        Libnet <pons@gmx.li>
    Re: Managing remote win32 hosts <goedicke@goedsole.com>
        mysql DBI ? relaced by NULL (Mike Solomon)
        Negating phrases <allanon@hotmail.com>
    Re: Negating phrases (Anno Siegel)
    Re: Negating phrases <thomas.haselberger@ucpmorgen.com>
    Re: Negating phrases <allanon@hotmail.com>
        novice programmer <anthony_r_au@yahoo.com.au>
    Re: Search and replace - mutliples on one line? <w.koenig@acm.org>
    Re: using CPAN.pm for local installs w/dependencies <nobull@mail.com>
        Weighed shuffle/select. Vocabulary test (James Kilfiger)
    Re: Weighed shuffle/select. Vocabulary test (Anno Siegel)
        what's the scope of global variable in cgi ? <perseus_medusa@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 13 May 2003 10:43:06 GMT
From: vek@station02.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: @Array - making it null
Message-Id: <slrnbc1j1q.2a2.vek@station02.ohout.pharmapartners.nl>

On Tue, 13 May 2003 03:14:07 GMT,
    Uri Guttman <uri@stemsystems.com> wrote:


>>>>>> "BV" == Balaji Venkataraman <dont-reply@this.address> writes:
>
>  BV> So now that the experts say that undef is not a good thing to do, I
>  BV> have a question.
>
>  BV> I've see people do this to read the whole file in one fell swoop:
>
>  BV> Code 1
>  BV> ------
>  BV> local $/ = undef;
>
>the undef is redundant. local $/ will set it to undef.
>


I've been searching the manual pages for this piec of information, but it
appears to be missing.  If you don't know that my $xx and local $xx also
sets the initial value everytime the statement is executed at runtime
you may be more comfortable setting the value explicitely to undef.



Villy


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

Date: 13 May 2003 11:29:06 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: @Array - making it null
Message-Id: <b9qku2$rrk$3@mamenchi.zrz.TU-Berlin.DE>

Villy Kruse <nobody> wrote in comp.lang.perl.misc:
> On Tue, 13 May 2003 03:14:07 GMT,
>     Uri Guttman <uri@stemsystems.com> wrote:
> 
> 
> >>>>>> "BV" == Balaji Venkataraman <dont-reply@this.address> writes:
> >
> >  BV> So now that the experts say that undef is not a good thing to do, I
> >  BV> have a question.
> >
> >  BV> I've see people do this to read the whole file in one fell swoop:
> >
> >  BV> Code 1
> >  BV> ------
> >  BV> local $/ = undef;
> >
> >the undef is redundant. local $/ will set it to undef.
> >
> 
> 
> I've been searching the manual pages for this piec of information, but it
> appears to be missing.  If you don't know that my $xx and local $xx also
> sets the initial value everytime the statement is executed at runtime
> you may be more comfortable setting the value explicitely to undef.

The run time behavior of "my" and "local" is described in perlsub, not
in the individual descriptions in perlfunc which only point to perlsub.
That should probably be amended.

Anno


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

Date: 13 May 2003 11:49:32 GMT
From: Jens.Toerring@physik.fu-berlin.de
Subject: accept() troubles
Message-Id: <b9qm4c$m0cn8$1@fu-berlin.de>


Hi,

  I just got hit by something I seem to be too dense today to figure
out myself. I have some typical server:

------8<----------------------------------------------

my $server = IO::Socket::INET->new( LocalPort => 8080,
                                    Type      => SOCK_STREAM,
                                    Reuse     => 1,
                                    Listen    => 16,
                                    Proto     => 'tcp' )
    or exit 1;

while ( $client = $server->accept( ) ) {
  FORK:
    if ( my $pid = fork ) {         # parent on success
        close $client;
    } elsif ( defined $pid ) {      # child
        serve( );
    } elsif ( $! == EAGAIN ) {      # (hopefully) recoverable fork problem
         sleep 1;
         redo FORK;
    } else {                        # fork failed too badly
        shutdown( $server, 2 );
        exit 1;
    }
}

print STDERR "accept() failed with: $!\n";

------8<----------------------------------------------

This has been working without any problems on more than a dozen of
machines but on another one it dies with the error message

accept() failed with: No child processes

after spawning the child process for the very first connection. This
is a bit mysterious because I don't have any idea why accept() should
fail with "No child processes" (error no. 10) at all (and yes, I am
pretty sure the error comes from the accept() call, if I print out $!
directly before calling accept() it has some other value). Has anybody
an idea what could be going wrong?
                                         Thanks, Jens
-- 
      _  _____  _____
     | ||_   _||_   _|        Jens.Toerring@physik.fu-berlin.de
  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring


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

Date: 13 May 2003 12:24:10 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: accept() troubles
Message-Id: <b9qo5a$7fa$2@mamenchi.zrz.TU-Berlin.DE>

 <Jens.Toerring@physik.fu-berlin.de> wrote in comp.lang.perl.misc:
> 
> Hi,
> 
>   I just got hit by something I seem to be too dense today to figure
> out myself. I have some typical server:
> 
> ------8<----------------------------------------------
> 
> my $server = IO::Socket::INET->new( LocalPort => 8080,
>                                     Type      => SOCK_STREAM,
>                                     Reuse     => 1,
>                                     Listen    => 16,
>                                     Proto     => 'tcp' )
>     or exit 1;
> 
> while ( $client = $server->accept( ) ) {
>   FORK:
>     if ( my $pid = fork ) {         # parent on success
>         close $client;
>     } elsif ( defined $pid ) {      # child
>         serve( );
>     } elsif ( $! == EAGAIN ) {      # (hopefully) recoverable fork problem
>          sleep 1;
>          redo FORK;
>     } else {                        # fork failed too badly
>         shutdown( $server, 2 );
>         exit 1;
>     }
> }
> 
> print STDERR "accept() failed with: $!\n";
> 
> ------8<----------------------------------------------

[snip]

I don't know what's going on, but you should realize that if serve()
ever returns to the child, the child(!) will continue to run another
copy of the accept() loop.  I don't believe that is intentional, so
put an exit() after serve().

Anno


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

Date: Tue, 13 May 2003 21:43:21 +1000
From: "Sisyphus" <kalinabears@hdc.com.au>
Subject: Re: bootstrap
Message-Id: <3ec0db69$0$10817@echo-01.iinet.net.au>


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote

<snip>
>
> Most libraries need some initialization before they can be used.  If
> a library is loaded dynamically (at run time), this must happen
> immediately after loading.  To accomplish this, XS decrees that
> an XS module named "Foo" must define a function "boot_Foo" which
> will be called before the user regains control.  This is what the
> bootstrap() function does.  The user of the module doesn't get
> to see its parameters or anything.
>
> One of the things this function can usefully do is check whether
> the library version is in the expected bracket.
>

Apparently so - though I'm not sure how.
I have version 0.191 of Win32. I opened Win32.pm and amended 'bootstrap
Win32;' to 'bootstrap Win32 $VERSION;'. I then ran a script that contained:
use warnings;
use Win32;
print "$Win32::VERSION\n";

I then altered the version number to 0.100 (and subsequently to 0.200) -
expecting I might get some compaint when I ran the script. But there was no
complaint. The script  faithfully produced the version number that I had
bestowed upon the module.

> > This would mean that a module author's sole purpose in bootstrapping
> > "$VERSION" would be to provide the user with a bootstrap parameter that
the
> > user could use (in whatever way the user wanted).
>
> No, $VERSION (or any bootstrap parameters) are only for the consumption
> of the module during startup.
>
> [snip]
>
> Anno

The original error message was :
Compress::Zlib object version 1.16 does not match bootstrap parameter 1.21
at .....

I'll know I've understood this (at least superficially) when I can
intentionally create that error message. I haven't managed to do this (apart
from building an executable with PAR) - but I haven't tried all that hard
yet.

I can't find that error message in perldiag - no matches at all for
"bootstrap".

It's time I played with it some more :-)

Cheers,
Rob




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

Date: Tue, 13 May 2003 13:43:29 +0200
From: Lechu  <kopetnik@s-pam-nie.yahoo.com>
Subject: Convert array into hash element
Message-Id: <jcm1cvcn2k2133j1uam0f3v3n8bv3t4s9t@4ax.com>

How to do this:

Given is an array with unknown number of elements, ie: 
@a = ('a','b','c','d','e');

I want to convert it into hash like:
$h={};
$h->{'a'}{'b'}{'c'}{'d'}{'e'} = 1

Can I do this if I dont know length of the array? And how?

Lech


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

Date: Tue, 13 May 2003 14:37:16 +0200
From: "Thomas Kratz" <nospam@nospam.de>
Subject: Re: Convert array into hash element
Message-Id: <3ec0eef5.0@juno.wiesbaden.netsurf.de>

DQoiTGVjaHUiIDxrb3BldG5pa0BzLXBhbS1uaWUueWFob28uY29tPiB3cm90ZS4uLg0KPiBIb3cg
dG8gZG8gdGhpczoNCj4gDQo+IEdpdmVuIGlzIGFuIGFycmF5IHdpdGggdW5rbm93biBudW1iZXIg
b2YgZWxlbWVudHMsIGllOiANCj4gQGEgPSAoJ2EnLCdiJywnYycsJ2QnLCdlJyk7DQo+IA0KPiBJ
IHdhbnQgdG8gY29udmVydCBpdCBpbnRvIGhhc2ggbGlrZToNCj4gJGg9e307DQo+ICRoLT57J2En
fXsnYid9eydjJ317J2QnfXsnZSd9ID0gMQ0KPiANCj4gQ2FuIEkgZG8gdGhpcyBpZiBJIGRvbnQg
a25vdyBsZW5ndGggb2YgdGhlIGFycmF5PyBBbmQgaG93Pw0KPiANCj4gTGVjaA0KDQpBbHRob3Vn
aCB0aGlzIHNsaWdodGx5IHNtZWxscyBsaWtlIGhvbWV3b3JrICg/KQ0KDQogICB1c2Ugc3RyaWN0
Ow0KICAgdXNlIHdhcm5pbmdzOw0KICAgdXNlIERhdGE6OkR1bXBlcjsNCg0KICAgbXkgQGFycmF5
ID0gJ2EnIC4uICdlJzsNCg0KICAgbXkgJGhhc2ggPSB7fTsNCiAgIG15ICRsYXN0cmVmID0gJGhh
c2g7DQoNCiAgIGZvcmVhY2ggbXkgJGVsZW0gKEBhcnJheSkgew0KICAgICAgJGxhc3RyZWYtPnsk
ZWxlbX0gPSAkZWxlbSBlcSAkYXJyYXlbLTFdID8gMSA6IHt9Ow0KICAgICAgJGxhc3RyZWYgPSAk
bGFzdHJlZi0+eyRlbGVtfTsNCiAgIH0NCg0KICAgcHJpbnQgRHVtcGVyKCRoYXNoKTsNCg0KDQoN
ClRob21hcw0K



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

Date: 13 May 2003 12:47:27 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Convert array into hash element
Message-Id: <b9qpgv$981$1@mamenchi.zrz.TU-Berlin.DE>

Lechu  <kopetnik@s-pam-nie.yahoo.com> wrote in comp.lang.perl.misc:
> How to do this:
> 
> Given is an array with unknown number of elements, ie: 
> @a = ('a','b','c','d','e');
> 
> I want to convert it into hash like:
> $h={};
> $h->{'a'}{'b'}{'c'}{'d'}{'e'} = 1
> 
> Can I do this if I dont know length of the array? And how?

    sub build_hash {
        my( undef, $val, @keys) = @_;
        if ( @keys ) {
            my $key = shift @keys;
            build_hash( $_[ 0]->{ $key}, $val, @keys);
        } else {
            $_[ 0] = $val;
        }
    }

    build_hash( my $h, 1, qw( a b c d e));

Anno


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

Date: Tue, 13 May 2003 17:15:35 +0530
From: "spanda" <spanda@cisco.com>
Subject: how to set INC globally
Message-Id: <1052826254.149656@sj-nntpcache-5>

Hi,

How can we set path in  @INC, so that it will be available globally.

I use
BEGIN {
    push ($INC, $PATH);
}

But this is only at perl script level.

Thanks
Subrat




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

Date: 13 May 2003 12:27:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: how to set INC globally
Message-Id: <b9qobl$7fa$3@mamenchi.zrz.TU-Berlin.DE>

spanda <spanda@cisco.com> wrote in comp.lang.perl.misc:
> Hi,
> 
> How can we set path in  @INC, so that it will be available globally.
> 
> I use
> BEGIN {
>     push ($INC, $PATH);
> }

Better written as

    use lib $PATH;

That works without BEGIN because "use" already happens at compile time.

> But this is only at perl script level.

perldoc perlrun, look for the environment variable PERL5LIB.

Anno


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

Date: Tue, 13 May 2003 13:59:57 +0200
From: "Pons" <pons@gmx.li>
Subject: Libnet
Message-Id: <b9qj8n$matls$1@ID-172702.news.dfncis.de>


perl -MCPAN -e 'install Bundle::CPAN' on a Redhat9.0 box

 .....
Bundle summary: The following items in bundle Bundle::CPAN had installation
problems:
  Bundle::libnet and the following items had problems during recursive
  bundle calls: Net::Cmd
 ....

Any help?

Thanks




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

Date: Tue, 13 May 2003 12:28:32 GMT
From: William Goedicke <goedicke@goedsole.com>
Subject: Re: Managing remote win32 hosts
Message-Id: <m3n0hrf2tx.fsf@mail.goedsole.com>

Dear MHW -

"MHW" <MHW@MHW.com> writes:

> I want the ability to pole for service availability (HTTP, Terminal
> Services, etc.) on a Windows Load Balanced Cluster (WLBS)

You should investigate netsaint (i.e. http://www.netsaint.org).

> and [then I want to] generate windows commands from the Linux server
> to remove hosts from the cluster.

In the past I solved this problem by creating a CGI script on the
servers that would execute arbitrary commands contained in the URL.

"What?!?!?!" you say; that's the worst security disaster possible!
The trick was that I encrypted the URL-embedded command with the
public key of each server I was controlling.  I used this in
combination with netsaint to automatically control services for
several hundred machines at a web-hosting firm.  It worked like a
charm.

We also used it for systems configuration management and diagnostics.
We kept a database of diagnostic commands which we ran against
machines when customers reported problems to isolate changes.
Consider commands: perl -v, cksum /etc/xinetd.conf and net services.

     Yours -      Billy

============================================================
     William Goedicke     goedicke@goedsole.com            
                          http://www.goedsole.com:8080      
============================================================

          Lest we forget:

A day without raw fish is like a day without sunshine.

	- William Goedicke


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

Date: 13 May 2003 04:36:42 -0700
From: mike_solomon@lineone.net (Mike Solomon)
Subject: mysql DBI ? relaced by NULL
Message-Id: <56568be5.0305130336.14772d10@posting.google.com>

I am using DBI and mysqlPP to insert into a database

If my data includes '?' they get replaced with NULL

I have tried prefixing ? with \ but that makes no difference

Any help to resolve this would be much appreciated

Sample script below:

use strict;

#connect to database
use DBI ();

#database connect info
my $host = "localhost";
my $database = "test";
my $user = "test";
my $password = "test";

 # Connect to the database.
my $g_dbh = DBI->connect("DBI:mysqlPP:database=$database;host=$host","$user","$password")
	 or die "$DBI::errstr";

my $sql = qq \
INSERT INTO datacodes (codetype, code, description,description2)
			VALUES ( 'mike ? mike' , '01' ,'MIKE ? TEST' ,'MIKE ??? TES' )
\;

my $g_data = $g_dbh->prepare($sql);
$g_data->execute();

Regards

Mike Solomon


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

Date: Tue, 13 May 2003 12:25:22 +0100
From: "Allanon" <allanon@hotmail.com>
Subject: Negating phrases
Message-Id: <b9qkn7$11r2@newton.cc.rl.ac.uk>

Hi

If I have code like this:

@array = ("Name is Billy","Name is Fred","Name is Freda","Name is Steve");

foreach(@array)
{
     print "$_\n" if /Name is [^Billy|^Fred]/;
}

What I want it to do is print out all lines, except those where the name is
Billy or Fred. The above doesn't work, as it doesn't print out the Freda
line.

Could you tell me what I'm doing wrong?

Thanks

Allanon




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

Date: 13 May 2003 11:37:36 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Negating phrases
Message-Id: <b9qle0$rrk$4@mamenchi.zrz.TU-Berlin.DE>

Allanon <allanon@hotmail.com> wrote in comp.lang.perl.misc:
> Hi
> 
> If I have code like this:
> 
> @array = ("Name is Billy","Name is Fred","Name is Freda","Name is Steve");
> 
> foreach(@array)
> {
>      print "$_\n" if /Name is [^Billy|^Fred]/;
> }
> 
> What I want it to do is print out all lines, except those where the name is
> Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> line.
> 
> Could you tell me what I'm doing wrong?

You're ignoring how character classes "[]" work in a regex.  Try this
instead (untested):

    print unless /Name is (?:Billy|Fred)/;

Anno


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

Date: Tue, 13 May 2003 13:38:21 +0200
From: Thomas Haselberger <thomas.haselberger@ucpmorgen.com>
Subject: Re: Negating phrases
Message-Id: <uof27rs9e.fsf@ucpmorgen.com>

"Allanon" <allanon@hotmail.com> writes:

> Hi
>
> If I have code like this:
>
> @array = ("Name is Billy","Name is Fred","Name is Freda","Name is Steve");
>
> foreach(@array)
> {
>      print "$_\n" if /Name is [^Billy|^Fred]/;
                                ^^^^^^^^^^^^^^
This is a char class, matching one of the given chars.

[^B] means match a char that is not B

btw, I don't know what the second occurencs of ^ does.

> }
>
> What I want it to do is print out all lines, except those where the name is
> Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> line.

negative lookahead could do the job:

     print "$_\n" if /Name is (?!Billy|Freda)/;

lg,
        tom


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

Date: Tue, 13 May 2003 13:49:32 +0100
From: "Allanon" <allanon@hotmail.com>
Subject: Re: Negating phrases
Message-Id: <b9qpkt$mbk@newton.cc.rl.ac.uk>


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:b9qle0$rrk$4@mamenchi.zrz.TU-Berlin.DE...
> Allanon <allanon@hotmail.com> wrote in comp.lang.perl.misc:
> > Hi
> >
> > If I have code like this:
> >
> > @array = ("Name is Billy","Name is Fred","Name is Freda","Name is
Steve");
> >
> > foreach(@array)
> > {
> >      print "$_\n" if /Name is [^Billy|^Fred]/;
> > }
> >
> > What I want it to do is print out all lines, except those where the name
is
> > Billy or Fred. The above doesn't work, as it doesn't print out the Freda
> > line.
> >
> > Could you tell me what I'm doing wrong?
>
> You're ignoring how character classes "[]" work in a regex.  Try this
> instead (untested):
>
>     print unless /Name is (?:Billy|Fred)/;

Yes, I guess that would work.. however, I wanted to handle the negation
purely inside the regular expression.

Allanon




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

Date: Tue, 13 May 2003 11:51:30 GMT
From: "Anthony" <anthony_r_au@yahoo.com.au>
Subject: novice programmer
Message-Id: <3ec0dc33@news.syd.ip.net.au>

hi

I am newie to perl, I need help creating a script . My objective is to
rename a file named ABCDEFF.TXT to
                                                        nnddmmyh.hmm

nn= number 0-9
dd= day
mm= month
y=year
h.h=hour
mm= minutes

The application runs under dos without long file names. So the formating as
you can see is 8.3 format....... Where do i start , what commands to i use.
will someone help me start my script, or give an eample...


thanks







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

Date: Tue, 13 May 2003 12:50:25 +0200
From: Winfried Koenig <w.koenig@acm.org>
Subject: Re: Search and replace - mutliples on one line?
Message-Id: <3EC0CDF1.6070802@acm.org>

Brad Walton wrote:
>>You probably want URI::Escape.
> 
> 
> This did not solve my problem. I am not concerned with the characters in the
> script, I am concerned with the characters they print to the HTML.
> 
> Example:
> 
> $name = "<Brad>";
> 
> When printed to HTML, this displays nothing, because it is in a 'tag'
> according to HTML. I have tried HTML-Parser-3.28, but was unsuccessful
> getting it to install (error in nmake). Is there a magical equation already
> in existence to deal with unsafe html characters (for output)?

You may use the encode_entities() routine from HTML::Entities.

Winfried Koenig



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

Date: 13 May 2003 12:06:33 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: using CPAN.pm for local installs w/dependencies
Message-Id: <u97k8vm7gm.fsf@wcl-l.bham.ac.uk>

Michael Budash <mbudash@sonic.net> writes:

> can somebody refresh my memory on using CPAN.pm to do local installs 
> w/dependencies? i've been doing the following, but often i get errors 
> saying the dependended-upon modules can't be found, even though they're 
> being installed along the way:

Check your PERL5LIB environment variable.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 13 May 2003 04:17:29 -0700
From: zeimusu@yahoo.co.uk (James Kilfiger)
Subject: Weighed shuffle/select. Vocabulary test
Message-Id: <a2119192.0305130317.77d792be@posting.google.com>

I've read a ton of articles here about shuffling, I learnt a lot about
Fisher-Yates that I didn't know know before, but they haven't quite
helped me.

I want to select an array in such a way that I favour some elements
over others.

What I really want to do is make spelling tests. I have a list of
words that students have to learn. I want make little tests from (say)
10 words, but to prevent cheating I want each student to have a
different set of words.

I can do that with the shuffle algorithm from the faq list or
something like it.

But say I want to favour words starting with 'A', or words from
chapter 5 (or whatever)

I imagine that I have some kind of weight attached to each word, the
higher the weight the more likely the word should be to appear in the
list.

I'm not planning on generating these tests in real time, so I don't
care that much about speed. Is there a straightforward way to do what
I want?

I hope I have explained my problem clearly, and I appreciate any
pointers you can give me.

James


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

Date: 13 May 2003 12:06:20 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Weighed shuffle/select. Vocabulary test
Message-Id: <b9qn3s$7fa$1@mamenchi.zrz.TU-Berlin.DE>

James Kilfiger <zeimusu@yahoo.co.uk> wrote in comp.lang.perl.misc:
> I've read a ton of articles here about shuffling, I learnt a lot about
> Fisher-Yates that I didn't know know before, but they haven't quite
> helped me.
> 
> I want to select an array in such a way that I favour some elements
> over others.
> 
> What I really want to do is make spelling tests. I have a list of
> words that students have to learn. I want make little tests from (say)
> 10 words, but to prevent cheating I want each student to have a
> different set of words.
> 
> I can do that with the shuffle algorithm from the faq list or
> something like it.
> 
> But say I want to favour words starting with 'A', or words from
> chapter 5 (or whatever)
> 
> I imagine that I have some kind of weight attached to each word, the
> higher the weight the more likely the word should be to appear in the
> list.
> 
> I'm not planning on generating these tests in real time, so I don't
> care that much about speed. Is there a straightforward way to do what
> I want?
> 
> I hope I have explained my problem clearly, and I appreciate any
> pointers you can give me.

That's the problem of generating a random sample with a given distribution
(instead of the usual uniform distribution).  For some theory see
Knuth, _The Art of Computer Programming_ Vol. 2, "Other types of random
quantities".

If you can use a rather rough approximation, there is a simple solution:
To make words X, Y and Z appear twice as often than the other words,
put X, Y and Z in the list twice and the others only once.  Pick a uniform
sample from that list.  For more complicated cases, those lists get
large pretty soon.

If that is too coarse, you can divide the words up into n lists and
assign a probability p[i] to each list (whose sum must be 1).  These
probabilities divide the unit interval into n subintervals of length
pi.  To draw a word, chose a random number between 0 and 1.  (It will
fall in the i'th interval with probability p[i].)  Determine the sub-
interval through (a variant of) logarithmic search, and draw a (uniformly)
random word from the corresponding list.

Anno


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

Date: Tue, 13 May 2003 19:40:12 +0800
From: "j" <perseus_medusa@hotmail.com>
Subject: what's the scope of global variable in cgi ?
Message-Id: <3ec0d907$1@newsgate.hknet.com>

Hi all ,

    i am changing a run alone perl script to a module which consist of many
global variable. I am going to use that module in cgi. So I want to ask will
the module's global variable be shared by all cgi perl script using it ?

Thanks.

Perseus




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

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.  

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


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