[22252] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4473 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 27 18:07:13 2003

Date: Mon, 27 Jan 2003 15:05:09 -0800 (PST)
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, 27 Jan 2003     Volume: 10 Number: 4473

Today's topics:
        Bijeenkomst Amsterdam Perl Mongers, Dinsdag 4 februari  (Johan Vromans)
    Re: byte array in perl, how? pack, unpack? java to perl (Ben Morrow)
    Re: byte array in perl, how? pack, unpack? java to perl <krahnj@acm.org>
    Re: Entering backslashed values into variables when rea (Ben Morrow)
    Re: fork confusion <goldbb2@earthlink.net>
    Re: IO::Socket - timeout not working <goldbb2@earthlink.net>
    Re: Match operator clears @_ in functions called in s// (Anno Siegel)
    Re: Match operator clears @_ in functions called in s// <hakonrk@fys.uio.no>
    Re: Match operator clears @_ in functions called in s// <bongie@gmx.net>
    Re: Match operator clears @_ in functions called in s// <goldbb2@earthlink.net>
        More random then other random <mememe@meme.com>
    Re: More random then other random <total_jnk@hotmail.com>
    Re: More random then other random <total_jnk@hotmail.com>
    Re: More random then other random <total_jnk@hotmail.com>
    Re: More random then other random <mememe@meme.com>
    Re: More random then other random <total_jnk@hotmail.com>
    Re: More random then other random <total_jnk@hotmail.com>
    Re: More random then other random <mjcarman@mchsi.com>
        Netscape 4.x on Mac-Pc Window Resize Problem <minh26@hotmail.com>
        Perl Library Path.. I need to change at runtime <mshavel@optonline.net>
    Re: Perl Library Path.. I need to change at runtime <w_ichmann@uni-wuppertal.de>
    Re: Question about 64bit Integers in Perl <abigail@abigail.nl>
        Regular Expression Question (SteveW)
    Re: Regular Expression Question <krahnj@acm.org>
    Re: Reverse Inheritance? <goldbb2@earthlink.net>
        Running a batch command with Location: <djmeyer1@us.ibm.com>
    Re: simple perl cgi server (Phishfry)
    Re: Variable naming convention <simon.oliver@nospam.umist.ac.uk>
        What is the maximum record length for DBM entries? <litalien@americasm01.nt.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 27 Jan 2003 20:52:15 +0100
From: jvromans@squirrel.nl (Johan Vromans)
Subject: Bijeenkomst Amsterdam Perl Mongers, Dinsdag 4 februari 2003
Message-Id: <m21y2ye5g0.fsf@phoenix.squirrel.nl>

[English version follows the dutch text]

Amsterdam.pm staat voor de "Amsterdamse Perl Mongers", een groep van
gebruikers van Perl. In tegenstelling tot wat de naam suggereert is
Amsterdam.pm niet beperkt tot alleen Amsterdam, maar functioneert, tot
er meer gebruikersgroepen in Nederland zijn, als Nederlandse
gebruikersgroep.

Amsterdam.pm organiseert informele bijeenkomsten waar Perl gebruikers
kunnen samenkomen en informatie en gebruikservaringen met betrekking
tot Perl kunnen uitwisselen. Deze bijeenkomsten vinden normaliter
plaats op elke eerste dinsdag van de maand. De voertaal binnen
Amsterdam.pm is in pricipe Nederlands, maar indien nodig zal Engels
worden gebruikt, b.v. om te communiceren met niet-Nederlandssprekende
aanwezigen.

De eerstvolgende bijeenkomst vindt plaats op dinsdag 4 februari van
20:00 tot 22:00 uur op het kantoor van XS4All, Eekholt 42, Diemen. 

Voor een routebeschrijving, zie
http://www.xs4all.nl/contact/routebeschrijving.html

Liefhebbers van een etentje vooraf kunnen tussen 17:45 en 18:15
verzamelen. Om 18:15 (écht om 18:15!) zoeken we een restaurantje in de
buurt om een hapje te eten.

Bezoek onze Web site http://amsterdam.pm.org voor meer details.

[English version]

Amsterdam.pm stands for the Amsterdam Perl Mongers. We're basically a
Perl user group. Despite its name, it is not local to the Amsterdam
environment, but it welcomes Perl mongers from all over the
Netherlands.

Amsterdam.pm organises informal meetings where Perl users can meet,
and exchange information and experiences with regard to using Perl.
The meetings are normally held every first Tuesday of the month.
Although the preferred language for communication is Dutch, English
will be spoken if necessary.

Our next meeting is Tuesday February 4th, from 20:00 till 22:00 at the
office of XS4All, Eekholt 42, Diemen.

See http://www.xs4all.nl/contact/routebeschrijving.html for directions to
get there.

Should you want to join some of us for dinner, please gather between
17:45 and 18:15. At 18:15 sharp we'll leave for a restaurant somewhere
nearby for dinner.

See http://amsterdam.pm.org for more details.

------------------------------------------------------------------------------
Johan Vromans                                             jvromans@squirrel.nl
Squirrel Consultancy                                  Haarlem, the Netherlands
http://www.squirrel.nl                  http://www.squirrel.nl/people/jvromans
PGP Key 2048/4783B14D   KFP = 65 44 CA 66 B3 50 0B 34  CE 0E FB CA 2D 95 34 D0
------------------------ "Arms are made for hugging" -------------------------





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

Date: Mon, 27 Jan 2003 19:24:21 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: byte array in perl, how? pack, unpack? java to perl
Message-Id: <b14115$an6$1@wisteria.csv.warwick.ac.uk>

vientoloco@msn.com (vientoloco) wrote:
>Hi, 
>This funtions are in java.
>I'll try to find similar functions in perl.
>Help, please :)
>Any tutorial?
>
>
>/**
>  * transforms a hexadecimal string to a byte array
>  * @return the byte array, or null if the passed String is null
>  * @exception IllegalArgumentException if the passed String is not
>haxadecimal encoded
>  */

Yuck, reminds me why I like Perl :)

perldoc -f hex
perldoc -f chr

>  /**
>   * transforms a byte array into a hexadecimal String
>   * @return the hexadecimal String, or null if the passed array is
>null
>   */

perldoc -f sprintf

(OT: doesn't Java have sprintf? I can't believe that mess is necessary...)

Ben


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

Date: Mon, 27 Jan 2003 22:09:36 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: byte array in perl, how? pack, unpack? java to perl
Message-Id: <3E35ADB9.4A7495BA@acm.org>

vientoloco wrote:
> 
> This funtions are in java.
> I'll try to find similar functions in perl.
> Help, please :)
> Any tutorial?
> 
> /**
>   * transforms a hexadecimal string to a byte array
>   * @return the byte array, or null if the passed String is null
>   * @exception IllegalArgumentException if the passed String is not
> haxadecimal encoded
>   */

In perl you usually don't work with byte arrays but with strings.


>  public static byte[] fromHex(String hex) {

sub fromHex {
    my $hex = $_[0];


>    if (hex == null) {
>      return null;
>    }

    return unless length $hex;


>    else if ((hex.length() % 2) != 0) {
>      throw new IllegalArgumentException("Hex String length must be a
> multiple of 2.");
>    }

    die "Hex String length must be a multiple of 2.\n" if length $hex %
2;


>    int length = hex.length() / 2;
>    byte[] result = new byte[length];
>    String hits = "0123456789ABCDEF";
>    String h = hex.toUpperCase();
> 
>    for (int i = 0; i < length; i++) {
>      char c = h.charAt(2 * i);
>      int index = hits.indexOf(c);
>      if (index == -1) {
>        throw new IllegalArgumentException("Hex String can't contain '"
> + c +"'");
>      }
> 
>      int j = 16 * index;
>      c = h.charAt((2 * i) + 1);
>      index = hits.indexOf(c);
>      if (index == -1) {
>        throw new IllegalArgumentException("Hex String can't contain '"
> + c +"'");
>      }

    die "Hex String can't contain '$1'" if $hex =~ /([^[:xdigit:]])/;


>      j += index;
> 
>      result[i] = (byte) (j & 0xFF);
>    }

    $hex =~ s/([[:xdigit:]]{2})/chr hex $1/ge;


>    return result;

    if ( wantarray ) {
        return split //, $hex;
    }
    else {
        return $hex;
    }


>  }


John
-- 
use Perl;
program
fulfillment


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

Date: Mon, 27 Jan 2003 19:18:22 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: Entering backslashed values into variables when read in from files
Message-Id: <b140lu$ahs$1@wisteria.csv.warwick.ac.uk>

hal@thresholddigital.com wrote:
>Brian McCauley wrote:
>
>> Hal Vaughan <hal@thresholddigital.com> writes:
><snip>
>>> set lf \012
>>> 
>>> I have a routine that goes through and parses these and returns the value
>>> in
>>> a string.  In this case, it returns the "\012" in the string.
>> 
>> No it does not.  It returns '\012' or "\\012".
>>
>
>Okay.  I'm a bit confused.  When I print it, I get \012 -- does that clarify 
>it at all?

No, it was perfectly clear :).  In Perl, "\012" gives you a one-character
string containing LF. '\012', "\\012", or reading \012 out of a file, all
give you four-character strings.

>I guess, in a way, that's what I'm trying to do.  But even if I have a 
>config file with as simple a line as:
>
>lf=\012
>
>then, according to what you say above (that Perl is interperting a line of 
>code differently than a line imported from a file), how can I tell Perl to 
>read \012 from a file as a control character?  The only way I can think of 
>is a lookup table so when it sees that 4 character sequence in the file, it 
>matches it to a key in a hash that contains the actual character.

No no no no, that's far too complicated.

>Isn't there a simple process for being able to read a line from a file that 
>contains \0xx and convert it to the actual control code?

perldoc -f chr
perldoc -f oct
perldoc -f hex

Ben


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

Date: Mon, 27 Jan 2003 16:13:55 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: fork confusion
Message-Id: <3E35A113.65DD1DF2@earthlink.net>

Philip Washington wrote:
> 
> I have the following code where I am trying to run a program for a
> given length of time. Kill it, run another program and restart the
> original.
> The first progam will run indefinitely if allowed.  I have not added
> the second program because I thus far have not been able to cleanly
> kill the first.
> 
> The programs which are run under exec(" ") or system(" ") appear to
> create another child, but when I run
> kill "INT",$kidpid
> they do not seem to be affected.
> 
> Any better ideas on how to accomlish this will be gratefully accepted.
[snip]

Try doing this [untested]:
   use IPC::Open2;
   use Symbol qw(gensym);
   my $first_prog = open2( ">&STDOUT", gensym, "sh", "Runfilter.sh" );
   sleep 10;
   KILLIT: {
      for my $sig ( qw(INT TERM KILL) ) {
         kill $sig => $first_prog and last KILLIT;
         sleep 1 if $sig ne "KILL";
      }
   }
   waitpid($first_prog, 0);
   my $second_prog = ....;
   
   


-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Mon, 27 Jan 2003 17:04:22 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: IO::Socket - timeout not working
Message-Id: <3E35ACE6.BFE16687@earthlink.net>

zen13097@zen.co.uk.invalid wrote:
> 
> I'm having problems geting timeouts working with IO::Socket.
> Take the sample code below - run it, then telnet to port 9999 on the
> same machine. The code prints a 'Connected' message, sets a 5 second
> timeout, and tries to read some data from the socket.
> 
> What's happening is that the timeout is being triggered (I'm seeing
> the "[timeout]" message from the signal handler) but the recv() call
> isn't exiting until it has received some data.

That's because perl sets up it's signal handlers to restart any system
calls which get interrupted.

> Am I doing something wrong?

Instead of using alarm and $SIG{ALRM}, use select to time out your
reception of data.  (Oh, and you should use sysread, not recv).


#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket;
use IO::Select;

my $listen = IO::Socket::INET->new(
   Proto       => 'tcp',
   LocalPort   => 9999,
   Listen      => 4,
   Reuse       => 1,
) or die "Can't create listen socket : $@\n";

while( 1 ) {
   my $client = $listen->accept or die "Accept failed: $!";
   if( IO::Select->new($client)->can_read(5) ) {
      sysread( $client, $_, 1 ) and print;
   } else {
      print "Timeout\n";
   }
   close $client;
}

PS: You should almost never be using either the <> operator or the
read() operator on any socket which you plan to use select() or
IO::Select with.  Doing so generally leads to deadlock.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: 27 Jan 2003 19:10:04 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Match operator clears @_ in functions called in s///e context
Message-Id: <b1406c$s16$2@mamenchi.zrz.TU-Berlin.DE>

Haakon Riiser  <hakonrk@fys.uio.no> wrote in comp.lang.perl.misc:
> [Jeff 'japhy' Pinyan]
> 
> > The regex in echo() sets (or in this case, unsets) all the $1, $2, $3,
> > etc. variables.  Because of the way Perl sends arguments to functions, the
> > $1 you send to the function is not the VALUE of $1, but the variable
> > itself.
> 
> Actually, I knew that the arguments are call-by-reference but it
> never occured to me that it could be the problem in this case --
> I've always thought that $1, $2, and so on were lexically scoped.

They are (sort of).  But the parameter passing mechanism transports an
alias of the outer variable into the inner (sub-) block.  The same
happens with a lexical instead of $1:

    my $x = 123; # instead of $1

    sub match_operation { # changes the value of $x
        $x = 456;
    }

    sub demo {
        match_operation();
        print "@_\n";
    }

This prints 456, the value $x is given in match_operation(), for the
same reason $1 was undefined in your original example.

Anno




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

Date: Mon, 27 Jan 2003 19:52:02 GMT
From: Haakon Riiser <hakonrk@fys.uio.no>
Subject: Re: Match operator clears @_ in functions called in s///e context
Message-Id: <slrnb3b3f2.bv2.hakonrk@s.hn.org>

[Anno Siegel]

> Haakon Riiser  <hakonrk@fys.uio.no> wrote in comp.lang.perl.misc:
>> Actually, I knew that the arguments are call-by-reference but it
>> never occured to me that it could be the problem in this case --
>> I've always thought that $1, $2, and so on were lexically scoped.
> 
> They are (sort of).  But the parameter passing mechanism transports an
> alias of the outer variable into the inner (sub-) block.  The same
> happens with a lexical instead of $1:
> [...]
> This prints 456, the value $x is given in match_operation(), for the
> same reason $1 was undefined in your original example.

After reading this, I tried putting "my $1" first in the echo()
function to see if that would prevent the match operator from
clobbering the parent scope's $1, but I wasn't allowed to do it:

  Can't use global $1 in "my" at foo.pl ....

I looked for it in main's symbol table ($main::{"1"}), and there it
was -- it had the expected value and everything.  Unfortunately,
the story doesn't end there -- it appears that only $1 can be
found in the symbol table, even though "my $2" and so on produces
the same warning as "my $1".

I guess I'll just have to accept that these numbered variables
are magical things with very special properties. :-)

-- 
 Haakon


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

Date: Mon, 27 Jan 2003 22:12:07 +0100
From: "Harald H.-J. Bongartz" <bongie@gmx.net>
Subject: Re: Match operator clears @_ in functions called in s///e context
Message-Id: <1902664.p0iDnFjfrs@nyoga.dubu.de>

Haakon Riiser wrote:
> After reading this, I tried putting "my $1" first in the echo()
> function to see if that would prevent the match operator from
> clobbering the parent scope's $1, but I wasn't allowed to do it:

Try with "local $1".  :-)

(They are globals and you are not allowed to turn them into lexicals,
but you can localize them.)

Ciao,
        Harald
-- 
Harald H.-J. Bongartz <bongie@gmx.net>
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Interesting Error Messages #12:
Backup not found! A)bort, R)etry or P)anic? 



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

Date: Mon, 27 Jan 2003 16:42:37 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Match operator clears @_ in functions called in s///e context
Message-Id: <3E35A7CD.C40C061C@earthlink.net>

Haakon Riiser wrote:
> 
> Take the following program:
> 
>   sub echo {
>       "x" =~ /x/;
>       print "(@_)\n";
>   }
> 
>   $0 =~ s/(.*)/echo($1)/e;
> 
> Run it with warnings enabled:
> 
>   Use of uninitialized value in join or string at foo.pl line 3.
[snip]

Others have told you why the problem occurs ... here is how to eliminate
the problem: Force perl to create a new scalar, so that $_[0] is an
alias to that new scalar, rather than an alias to $1.

Either of the following should work:
    $0 =~ s/(.*)/echo("$1")/e;
    $0 =~ s/(.*)/echo(my $tmp = $1)/e;

Or, alternatively, change sub echo to make and use a copy of @_, rather
than using it directly:
   sub echo {
      my @args = @_;
      "x" =~ /x/;
      print "(@args)\n";
   }

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Mon, 27 Jan 2003 13:56:49 -0700
From: "ColdCathoids" <mememe@meme.com>
Subject: More random then other random
Message-Id: <b146ei$v5ece$1@ID-158028.news.dfncis.de>

Hi all,

This might sound like an odd post but please bear with me. I will start by
explaining my situation.

I am working on a website for a client where users can enter ads (all in
Perl with a MySql backend). The admins of the site have the ability to rate
ads from 1 to 10. On the front page of the site is the "Featured Ad". The
way it is now I just select a random entry from the ads table and have it
displayed. What the client wants is for ads rated 10 to appear much more
often then ads rated lower.

I thought about this for awhile and devised a ghetto way of doing it. I
would create a table that contains one column. There would be the number 10
entered 10 times, 9 entered 9 times, and so on. I would then randomly grab a
row from this table. Then based on that select all the ads with the
corresponding rating and get a random one from those. I know this will work
but it seems plain ugly to me.

"What does this have to do with perl?" you are probably asking. Well, I was
wondering if there is a way in perl that I could accomplish this. Is there a
way to do a quasi-random selection? Random .. yet have some things less
random?

Any help would be most appreciated.




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

Date: Mon, 27 Jan 2003 16:45:57 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: More random then other random
Message-Id: <b149am$veuhi$1@ID-121117.news.dfncis.de>

ColdCathoids wrote:
> Hi all,
> I thought about this for awhile and devised a ghetto way of doing it. I
> would create a table that contains one column. There would be the number 10
> entered 10 times, 9 entered 9 times, and so on. I would then randomly grab a
> row from this table. Then based on that select all the ads with the
> corresponding rating and get a random one from those. I know this will work
> but it seems plain ugly to me.

You need 10 buckets where each bucket differs in size according to your 
requirements of ad frequency.

Have an array with the bucket sizes in it and select a random number up 
to the total of the sizes and see where it falls:

@bucket = (1,2,3,4,5,6,7,8,9);
$maxrand = $_ foreach (@bucket);
$r = rand($maxrand)+1;
$s = 0;
for ($i=0; $i<@bucket; $i++) {
    $s += $i;
    last if $s > $r;
}

print "bucket $i\n";

Since different categories have different amounts of numbers associated 
with them you get them coming up more or less frequently according to 
your own choice. After you find which add category to choose you just 
have to randomly pick one of them.

regards,
Ben



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

Date: Mon, 27 Jan 2003 16:48:47 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: More random then other random
Message-Id: <b149fv$veuhi$2@ID-121117.news.dfncis.de>

Ben wrote:
> @bucket = (1,2,3,4,5,6,7,8,9);
> $maxrand = $_ foreach (@bucket);

That should actually read:
    $maxrand += $_ foreach (@bucket);

> $r = rand($maxrand)+1;
> $s = 0;
> for ($i=0; $i<@bucket; $i++) {
>    $s += $i;
>    last if $s > $r;
> }
> 
> print "bucket $i\n";
> 



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

Date: Mon, 27 Jan 2003 17:16:56 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: More random then other random
Message-Id: <b14b4p$v5c6s$1@ID-121117.news.dfncis.de>

Ben wrote:
> Ben wrote:
> 
>> @bucket = (1,2,3,4,5,6,7,8,9);
>> $maxrand = $_ foreach (@bucket);
> 
> 
> That should actually read:
>    $maxrand += $_ foreach (@bucket);
> 
>> $r = rand($maxrand)+1;
>> $s = 0;
>> for ($i=0; $i<@bucket; $i++) {
>>    $s += $i;

Argh. And that should be:
     $s += $bucket[$i];

I.e.:

@bucket = (1,2,3,4,5,6,7,8,9);
$total += $_ foreach (@bucket);
print "total = $total\n";
$r = rand($total)+1;
$s = 0;
for ($i=0; $i<@bucket; $i++) {
    $s += $bucket[$i];
    print "$i. rand=$r  sum=$s \n";
    last if $s > $r;
}

print "bucket $i\n";




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

Date: Mon, 27 Jan 2003 15:28:36 -0700
From: "ColdCathoids" <mememe@meme.com>
Subject: Re: More random then other random
Message-Id: <b14bsk$u7m96$1@ID-158028.news.dfncis.de>


"Ben" <total_jnk@hotmail.com> wrote in message
news:b14b4p$v5c6s$1@ID-121117.news.dfncis.de...
> Ben wrote:
> > Ben wrote:
> >
> >> @bucket = (1,2,3,4,5,6,7,8,9);
> >> $maxrand = $_ foreach (@bucket);
> >
> >
> > That should actually read:
> >    $maxrand += $_ foreach (@bucket);
> >
> >> $r = rand($maxrand)+1;
> >> $s = 0;
> >> for ($i=0; $i<@bucket; $i++) {
> >>    $s += $i;
>
> Argh. And that should be:
>      $s += $bucket[$i];
>
> I.e.:
>
> @bucket = (1,2,3,4,5,6,7,8,9);
> $total += $_ foreach (@bucket);
> print "total = $total\n";
> $r = rand($total)+1;
> $s = 0;
> for ($i=0; $i<@bucket; $i++) {
>     $s += $bucket[$i];
>     print "$i. rand=$r  sum=$s \n";
>     last if $s > $r;
> }
>
> print "bucket $i\n";
>
Hmmm ... I see where you are going with this. Only thing is it doesn't seem
to hit the highest number too often. 8 seems to be the random number quite a
lot more then 9.




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

Date: Mon, 27 Jan 2003 17:55:42 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: More random then other random
Message-Id: <b14ddf$v11sl$1@ID-121117.news.dfncis.de>

ColdCathoids wrote:
> "Ben" <total_jnk@hotmail.com> wrote in message
>>@bucket = (1,2,3,4,5,6,7,8,9);
>>$total += $_ foreach (@bucket);
>>print "total = $total\n";
>>$r = rand($total)+1;
>>$s = 0;
>>for ($i=0; $i<@bucket; $i++) {
>>    $s += $bucket[$i];
>>    print "$i. rand=$r  sum=$s \n";
>>    last if $s > $r;
>>}
>>
>>print "bucket $i\n";
>>
> 
> Hmmm ... I see where you are going with this. Only thing is it doesn't seem
> to hit the highest number too often. 8 seems to be the random number quite a
> lot more then 9.

Fixed the rand:

sub ad {
    my ($r, $s, $i);

    $r = rand($total);
    $s = 0;
    for ($i=0; $i<@bucket; $i++) {
       $s += $bucket[$i];
       last if $s > $r;
    }

    return $i;
}

@bucket = (1,2,3,4);
$total += $_ foreach (@bucket);

# select 10000 ad categories
for ($i=0; $i<10000; $i++) {
     $count[&ad]++;
}

# print results
for ($i=0; $i<@count; $i++) {
     print "$i. hits $count[$i]\n";
}



This looks right:
0. hits 1001
1. hits 1972
2. hits 3001
3. hits 4026

With (1,2,3,4,5,6,7,8,9) output is:
0. hits 203
1. hits 455
2. hits 695
3. hits 900
4. hits 1120
5. hits 1297
6. hits 1612
7. hits 1725
8. hits 1993



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

Date: Mon, 27 Jan 2003 18:01:02 -0500
From: Ben <total_jnk@hotmail.com>
Subject: Re: More random then other random
Message-Id: <b14dnf$v2m0a$1@ID-121117.news.dfncis.de>

Ben wrote:
>>>    last if $s > $r;

Probably >= but you get the idea.



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

Date: Mon, 27 Jan 2003 16:09:44 -0600
From: Michael Carman <mjcarman@mchsi.com>
Subject: Re: More random then other random
Message-Id: <b14anh$3uh1@onews.collins.rockwell.com>

On 1/27/03 2:56 PM, ColdCathoids wrote:
> 
> I am working on a website for a client where users can enter ads. The
> admins of the site have the ability to rate ads from 1 to 10. On the
> front page of the site is the "Featured Ad". [...] the client wants
> is for ads rated 10 to appear much more often then ads rated lower.

You want a weighted distribution dependant on an ad's rating.

> I would create a table that contains one column. There would be the
> number 10 entered 10 times, 9 entered 9 times, and so on. I would 
> then randomly grab a row from this table. Then based on that select 
> all the ads with the corresponding rating and get a random one from 
> those. I know this will work but it seems plain ugly to me.

That depends on what you mean by "work." Let's say you have only one
really crummy ad with a rating of "1," but lots of great ads (100) with
a rating of "10." By your system, you're 10 times more likely to see a
"10" than a "1" but _every time_ you see a "1" it will be the same
crummy ad, while you'll have only 1 change in 100 of seeing a particular
ad from the list of "10s."

In other words, you'll see your worst rated ad 10 times as often as any
particular top-ranking ad.

Of course, different distrutions will yield different results, but you
get the idea. What you (presumably) want to do is make each "10" 10 x
more likely to appear than any particular "1" regardless of how your
ratings are distributed. To do that, you have to weight the probability
of any particular ad being picked. Here's a simple example:

    #!/usr/bin/perl -w
    use strict;

    my @rating = (1,5,10,3,5);
    my @dist = map {($_) x $rating[$_]} (0 .. $#rating);

    for (1 .. 20) {
        print $dist[rand(@dist)], "\n";
    }

The indices of @rating are ad IDs and the values are the respective
ratings. i.e. ad #0 has a rating of 1, ad #2 has a rating of 5, etc.

@dist contains a mapping of the indices of @rating (the ad IDs) such
that highly ranked ads appear more often than poorly ranked ones. If you
want to tweak the weighting, just change the map{} block. e.g. to make
higher ranked ads exponentially more likely than lower ranked ones:

    my @dist = map {($_) x ($rating[$_]**2)} (0 .. $#rating);

After that, it's a simple matter of picking a random element out of @dist.

-mjc



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

Date: Mon, 27 Jan 2003 14:39:53 -0500
From: "Minh V. Tran" <minh26@hotmail.com>
Subject: Netscape 4.x on Mac-Pc Window Resize Problem
Message-Id: <b141pn$hi$1@biker.larc.nasa.gov>

Greetings,

Believe me before I post this question, I've read many articles similar to
the problem I have, but I can't find a solution for my problem. I post this
problem with a hope you JavaScript experts can help me out on this matter.

I have a website written in Perl and JavaScript. It has layers and frames
features. The problem arises when I resize the window on Netscape 4.x. The
problem always occurs when I use Netscape 4.x on Mac. For PC, I can produce
the error if I want to by resize the window repeatedly.
To avoid this problem, the application disables all pop-up windows except
the parent window, I guess you understand why. Whenever I resize the window,
this is the message I have:

"Data Missing

This document resulted from a POST operation and has expired from the cache.
If you wish you can repost the form data to recreate the
document by pressing the reload button. "

If I reload the page by pressing the reload button, all entered data will be
lost. Can you guys give me some hints how to resolve this problem, websites
about this is also very helpful to me.

Thanks in advance,
Minh
--




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

Date: Mon, 27 Jan 2003 21:17:17 GMT
From: Mike <mshavel@optonline.net>
Subject: Perl Library Path.. I need to change at runtime
Message-Id: <Pine.GSO.4.10.10301271605390.574-100000@tigris>

Hello everyone, 

I have a solaris 8 server that came  with perl installed.  For various
reasons I needed to install my own version of perl in /usr/local.   Now, I
have a script that needs to run the /usr/local perl as well as the
libraries under another  path.   Can I change a variable in the perl
script to
point to the library path I want to use for that script? I assume I can.
Can someone tell me what that ENV variable is and how I would set it in
the perl script. 

Thanks very much

Mike





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

Date: Mon, 27 Jan 2003 23:31:02 +0100
From: Ingo Wichmann <w_ichmann@uni-wuppertal.de>
Subject: Re: Perl Library Path.. I need to change at runtime
Message-Id: <b14c74$3ed$01$1@news.t-online.com>

Mike schrieb:
> I have a solaris 8 server that came  with perl installed.  For various
> reasons I needed to install my own version of perl in /usr/local.   Now, I
> have a script that needs to run the /usr/local perl as well as the
> libraries under another  path.   Can I change a variable in the perl
> script to
> point to the library path I want to use for that script? I assume I can.
> Can someone tell me what that ENV variable is and how I would set it in
> the perl script. 

I've never done this. But you can use "use lib '/xx/yy'" to set it at 
compile time. Maybe perldoc lib helps you.

Ingo

-- 
Meine Mail-Adresse enthält keinen Unterstrich



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

Date: 27 Jan 2003 21:58:38 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Question about 64bit Integers in Perl
Message-Id: <slrnb3base.pj6.abigail@alexandra.abigail.nl>

doug (doug.portz@mts.mb.ca) wrote on MMMCDXXXVI September MCMXCIII in
<URL:news:bff1479d.0301270904.13bcb2fa@posting.google.com>:
\\  Hi,
\\  
\\  let me say right off the bat, I know nothing about Perl.... but I do
\\  have a question I am hoping someone can answer.
\\  
\\  We currently have a Perl program that manages an SNMPT application. 
\\  we wantto change from 32 bit to 64 bit integers - which we can do
\\  pretty easy. but I have heard that getting perl to accept a 64bit
\\  integer may be difficult.
\\  
\\  Can someone confirm with me whether it is easy to change a Perl
\\  program to accept a 64 bit integer, if so are there limitations?


What do you mean by "accept a 64 bit integer"? Perl compiled with
64 bit integer support handles 64 bit integers just fine.



Abigail
-- 
BEGIN {$^H {q} = sub {pop and pop and print pop}; $^H = 2**4.2**12}
"Just "; "another "; "Perl "; "Hacker\n";


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

Date: 27 Jan 2003 13:23:28 -0800
From: spw57nc@hotmail.com (SteveW)
Subject: Regular Expression Question
Message-Id: <b11ffee8.0301271323.4c46b29e@posting.google.com>

I've been looking at example 9.15 in Dave Roth's "Win32 Perl
Scripting:  The Administrator's Handbook" and I've got a question.  He
uses a regular expression

  my( $Regex ) = ( $Name =~ m#[$^\\/*?{}\[\]]+# );

that is described as "If a process name is passed in, all processes
matching the name are terminated.  If the name contains wildcards such
as * and ?, it is used as a regular expression." (p. 342)

When I try using this routine, passing in a process name, I get the
following error:
"Use of uninitialized value in concatenation (.) or string"

Can anyone explain to me what this regular expression is doing, and
why I get an error when I pass in a process name?

Thanks.


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

Date: Mon, 27 Jan 2003 22:25:12 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Regular Expression Question
Message-Id: <3E35B161.D0586740@acm.org>

SteveW wrote:
> 
> I've been looking at example 9.15 in Dave Roth's "Win32 Perl
> Scripting:  The Administrator's Handbook" and I've got a question.  He
> uses a regular expression
> 
>   my( $Regex ) = ( $Name =~ m#[$^\\/*?{}\[\]]+# );
> 
> that is described as "If a process name is passed in, all processes
> matching the name are terminated.  If the name contains wildcards such
> as * and ?, it is used as a regular expression." (p. 342)
> 
> When I try using this routine, passing in a process name, I get the
> following error:
> "Use of uninitialized value in concatenation (.) or string"
> 
> Can anyone explain to me what this regular expression is doing,

Yes, the regular expression is a character class matching one or more
(this is redundant as matching only one will accomplish the same thing)
of either '$' or '^' or '\' or '/' or '*' or '?' or '{' or '}' or '[' or
']'.  If one of these characters is found in $Name then $Regex is
assigned 1 (true) or if not then $Regex is assigned 0 (false).


> and why I get an error when I pass in a process name?

The error message is from some other line in the program as there is no
concatenation involved in the line you provided.



John
-- 
use Perl;
program
fulfillment


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

Date: Mon, 27 Jan 2003 15:57:17 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Reverse Inheritance?
Message-Id: <3E359D2D.E167B9D6@earthlink.net>

Anno Siegel wrote:
> Abigail wrote:
[snip]
> > BTW, note that 'require "$backend.pm"' is going to fail if $backend
> > contains 'Foo::Whatever'. You'd have to parse $backend, and
> > substitute the right path separator for '::'.
> 
> True.  I didn't nocice this introduces a system-dependency.  Why can't
> we just all get along and use Unix... :)

Actually, require "Foo/Bar.pm" will do the right thing, no matter what
your system's directory seperator's are.

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: Mon, 27 Jan 2003 14:11:54 -0600
From: David Meyer <djmeyer1@us.ibm.com>
Subject: Running a batch command with Location:
Message-Id: <3E35928A.3010800@us.ibm.com>

Hello,

In a CGI on apache (Redhat 8), I'm trying
to start a script in the background which
updates an .html file.  After starting the
batch job, I print a Location: pointing at
the html file which is updating every 8
seconds.

The problem is that the html file does not
show up until the batch job is complete. I'd
like to be able to refresh a growing html file.
I had this working awhile back on AIX but am
having problems on Redhat.....any help is
much appreciated.

Here's a code fragment:

   $cmd="$CGIdir/batchtest &";
   system($cmd) && exit();
   print("Location: $RunTestHTML_URL\n\n");

Thanks,
djmeyer@bga.com



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

Date: 27 Jan 2003 11:38:58 -0800
From: Phishfry@my-deja.com (Phishfry)
Subject: Re: simple perl cgi server
Message-Id: <92f0dc53.0301271138.2d4e9a25@posting.google.com>

"Adam Kumpf" <kumpf@mit.edu> wrote in message news:<3e2d7697$0$3948$b45e6eb0@senator-bedfellow.mit.edu>...
> Thanks for everyone's input.. I got it up and working in under 2 hours.  : )
> 
> Best Regards,
>     Adam Kumpf
> kumpf@mit.edu
> 
Adam
What option did you choose, for quick webserver?


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

Date: Mon, 27 Jan 2003 12:28:24 +0000
From: Simon Oliver <simon.oliver@nospam.umist.ac.uk>
Subject: Re: Variable naming convention
Message-Id: <3E3525E8.4070305@nospam.umist.ac.uk>

Pete wrote:
> Is there a way to dynamically define a set of variable names such that
> the name itself consists of alpha and numeric characters, but where
> the alpha portion remains the same and the numeric portion changes for
> based on numbers in a loop or range?  If the loop or range of numbers
> is say ($num=1, $num<=10, $num++), and the base variable name is
> $name, then I want my actual variable names to be like this: $name1,
> $name2, $name3, etc.

You probably want a hash, something like:

my %hash = ();

for (my $i=1; $i<=10; $i++) {
   $hash{"$name$i"} = $default;
}

Of course hash elements are created dynamically, so you don't need to 
pre-populate the hash as above:

my %hash2;
$hash2{anothername99} = 'asandwhen';

-- 
   Simon Oliver



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

Date: Mon, 27 Jan 2003 15:04:52 -0500
From: "Litalien, Robert [SKY:7Q23:EXCH]" <litalien@americasm01.nt.com>
Subject: What is the maximum record length for DBM entries?
Message-Id: <b143b2$1gn$1@zcars0v6.ca.nortel.com>

I have a question about DBM files.

I've been looking at Perl documentation on the web and cannot find any
length limits for a value assigned to a DBM key (not the length of the key
itself but its value).  I have a small DB of about 15 entries (keys) but
their value can be significant.

Would someone know the length limits of such an entry?

Thanks you in advance.

Robert




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

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


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