[22066] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4288 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 19 09:11:17 2002

Date: Thu, 19 Dec 2002 06:10:16 -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           Thu, 19 Dec 2002     Volume: 10 Number: 4288

Today's topics:
        Replacing text strings contains more than 1 dot (Eric)
    Re: Replacing text strings contains more than 1 dot <bernard.el-hagin@DODGE_THISlido-tech.net>
    Re: splitting array, each element holding multiple valu <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
    Re: splitting array, each element holding multiple valu (Anno Siegel)
    Re: Strange behaviour with windows task scheduler <Nick_Djurovich@NOSPAM.aimtechnology.com>
    Re: Strange behaviour with windows task scheduler <s.patterson@freeuk.com>
        Strange loop (i5513)
    Re: Strange loop <krahnj@acm.org>
    Re: Strange loop <bernard.el-hagin@DODGE_THISlido-tech.net>
    Re: Strange loop (Anno Siegel)
    Re: Tracking User's OS type? <flavell@mail.cern.ch>
        trapping connection timeout ... using sockets <leo@jollibee.balei.org>
    Re: trapping connection timeout ... using sockets (Anno Siegel)
    Re: Why do beginners read files into an array? (Helgi Briem)
    Re: Why do beginners read files into an array? <me@privacy.net>
    Re: Why is system call exit code factor of 256? <me@privacy.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 19 Dec 2002 03:57:13 -0800
From: eric.chin@pinnacle.co.uk (Eric)
Subject: Replacing text strings contains more than 1 dot
Message-Id: <d8c847cd.0212190357.34282b4@posting.google.com>

Greetings,

I would like to replace some ip addresses in a text file.

I try:

perl -pi -e 's/111.222.333.444/111.222.333.999/e' filename

I gets: 111.222333.999

In my case, I can get away by doing 

perl -pi -e 's/333.444/333.999/e' filename

I wonder, how do I do the search and replace using the full ip address
to ensure uniqueness ?

Any pointer appreciated.

TIA

Eric


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

Date: Thu, 19 Dec 2002 11:59:25 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: Replacing text strings contains more than 1 dot
Message-Id: <atscas$6lr$1@korweta.task.gda.pl>

In article <d8c847cd.0212190357.34282b4@posting.google.com>, Eric
wrote:
> Greetings,
> 
> I would like to replace some ip addresses in a text file.
> 
> I try:
> 
> perl -pi -e 's/111.222.333.444/111.222.333.999/e' filename


Get rid of the /e and backslash the .s in the regex:


s/111\.222\.333\.444/111.222.333.999/;


And check out:


perldoc perlop


for why you got incorrect results with /e.



Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'


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

Date: Thu, 19 Dec 2002 09:52:58 +0100
From: Koos Pol <koos_pol@NO.nl.JUNK.compuware.MAIL.com>
Subject: Re: splitting array, each element holding multiple values into array with single values 
Message-Id: <newscache$aczc7h$80m$1@news.emea.compuware.com>

hugo wrote (Thursday 19 December 2002 08:54):

> Hi
> 
> I have a problem I don't seem to be able to solve.
> 
> I have a cgi script creating a page with a form containing a series of
> text boxes, each box containing a multi-selection list.
> 
> When the form is forwarded I get the values out as follows:
> 
> for ($i = 0; $i < $projectlimit; $i++)  {
>     $P[$i] = join ' ' => $query->param("company_projects$i");
> }


    Do you actually used these joined values in this way?
    If not, why join them in the first place?


> 
> so that I end up with an array, each array element containing a set of
> values.
> 
> However, I would like to split the values into another array which,
> presumably, has to be two dimensional. In other words, if my array @P
> contains
> 
> $P[0] = "A B C D";
> $P[1] = "X Y Z";
> 
> then my new array should be:
> 
> $NP[0][0] = "A";
> $NP[0][1] = "B";
> $NP[0][2] = "C";
> $NP[0][3] = "D";
> $NP[1][0] = "X";
> $NP[1][1] = "Y";
> $NP[1][2] = "Z";


    Let the CGI module handle your query params. It already returns
    the multi select options in an array (or a hash at your
    preference). And then you can get away with just this:

    <untested>
    for ($i = 0; $i < $projectlimit; $i++)  {
        @{ $P[$i] } = $query->param("company_projects$i");
    }


-- 
KP



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

Date: 19 Dec 2002 09:01:39 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: splitting array, each element holding multiple values into array with single values
Message-Id: <ats1tj$1dj$1@mamenchi.zrz.TU-Berlin.DE>

According to hugo  <hugo@geoinformex.com>:
> Hi
> 
> I have a problem I don't seem to be able to solve.
> 
> I have a cgi script creating a page with a form containing a series of 
> text boxes, each box containing a multi-selection list.
> 
> When the form is forwarded I get the values out as follows:
> 
> for ($i = 0; $i < $projectlimit; $i++)  {
>     $P[$i] = join ' ' => $query->param("company_projects$i");
> }
> 
> so that I end up with an array, each array element containing a set of 
> values.
> 
> However, I would like to split the values into another array which, 
> presumably, has to be two dimensional. In other words, if my array @P 
> contains
> 
> $P[0] = "A B C D";
> $P[1] = "X Y Z";
> 
> then my new array should be:
> 
> $NP[0][0] = "A";
> $NP[0][1] = "B";
> $NP[0][2] = "C";
> $NP[0][3] = "D";
> $NP[1][0] = "X";
> $NP[1][1] = "Y";
> $NP[1][2] = "Z";
> 
> How can I do this?

Instead of joining the elements that come out of param(), make an
anonymous array out of them, and assign that to @NP directly:

    for ($i = 0; $i < $projectlimit; $i++)  {
        $NP[$i] = [ $query->param("company_projects$i") ];
    }

or even

    @NP = map [ $query->param( "company_project$_") ],
        0 .. $projectlimit - 1;

That should be all.

Anno


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

Date: Thu, 19 Dec 2002 10:33:42 +0000 (UTC)
From: "Nick Djurovich" <Nick_Djurovich@NOSPAM.aimtechnology.com>
Subject: Re: Strange behaviour with windows task scheduler
Message-Id: <ats7a6$eel$1@venus.btinternet.com>

"Stephen Patterson" <s.patterson@freeuk.com> wrote in message
news:slrnb01olf.pi.s.patterson@bloodnok.localdomain...
> I have a couple of scripts which are invoked by the task scheduler on
> windows 2000, and which occasionally fail to run for no apparent reason
> (i.e. it works fine if I run it myself).
>
> I've adjusted the scripts to create log files, from which I can tell that
> when the scripts fail, the task scheduler is not executing them.
>
> Any ideas?  The scripts are on a network drive, could this be affecting
> things?

Hi Stephen,

Since they only occasionally fail, i would suggest 'sprinkling' some debug
information into a file, to see if it's running the script at all, or to
find
out where in the script it's failing.

Regards

Nick




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

Date: 19 Dec 2002 11:55:43 GMT
From: Stephen Patterson <s.patterson@freeuk.com>
Subject: Re: Strange behaviour with windows task scheduler
Message-Id: <slrnb03ctt.jkb.s.patterson@seagoon.localdomain>

On Thu, 19 Dec 2002 10:33:42 +0000 (UTC), Nick Djurovich wrote:
> "Stephen Patterson" <s.patterson@freeuk.com> wrote in message
> news:slrnb01olf.pi.s.patterson@bloodnok.localdomain...
>> I have a couple of scripts which are invoked by the task scheduler on
>> windows 2000, and which occasionally fail to run for no apparent reason
>> (i.e. it works fine if I run it myself).
> Since they only occasionally fail, i would suggest 'sprinkling' some debug
> information into a file, to see if it's running the script at all, or to
> find
> out where in the script it's failing.

I've tried that, the very first thing the script does now is open and
timestamp the logfile (if it runs).

I've moved the scripts onto the hard drive and checked the password for the
user they run under so I'll see how it goes.

-- 
Stephen Patterson http://www.lexx.uklinux.net http://patter.mine.nu
steve@SPAM.lexx.uklinux.net  remove SPAM to reply        
Linux Counter No: 142831 GPG Public key: 252B8B37        
Last one down the pub's an MCSE


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

Date: 19 Dec 2002 01:53:22 -0800
From: i5513@hotmail.com (i5513)
Subject: Strange loop
Message-Id: <a657ec02.0212190153.207f6be1@posting.google.com>

Hello again, we have a problem with decimal numbers.
When we have next loops, we don't get the result expected
for ($i = 0.001; $i <= 0.01 ;$i += 0.001)
{
	print $i." ";
}
print "\n";
# OUT: 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 WITHOUT
0.01!!!
for ($i = 0.01; $i <= 0.1 ; $i += 0.01)
{
	print $i." ";
}
print "\n";
# OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1 ¡OK!
for ($i = 0.01; $i < 0.1 ; $i += 0.01)
{
	print $i." ";
}
# OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1!!!!!
(look at ¡¡¡¡<!!!)

Where is the problem???? We don't know
Thanks!


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

Date: Thu, 19 Dec 2002 10:29:30 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Strange loop
Message-Id: <3E019F41.32F73899@acm.org>

i5513 wrote:
> 
> Hello again, we have a problem with decimal numbers.
> When we have next loops, we don't get the result expected
> for ($i = 0.001; $i <= 0.01 ;$i += 0.001)
> {
>         print $i." ";
> }
> print "\n";
> # OUT: 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 WITHOUT
> 0.01!!!
> for ($i = 0.01; $i <= 0.1 ; $i += 0.01)
> {
>         print $i." ";
> }
> print "\n";
> # OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1 ¡OK!
> for ($i = 0.01; $i < 0.1 ; $i += 0.01)
> {
>         print $i." ";
> }
> # OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1!!!!!
> (look at ¡¡¡¡<!!!)
> 
> Where is the problem???? We don't know

This problem is explained in the FAQ

Found in /usr/lib/perl5/5.6.0/pod/perlfaq4.pod
       Why am I getting long decimals (eg, 19.9499999999999)
       instead of the numbers I should be getting (eg, 19.95)?



John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 19 Dec 2002 10:43:13 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: Strange loop
Message-Id: <ats7s1$bva$1@korweta.task.gda.pl>

In article <a657ec02.0212190153.207f6be1@posting.google.com>, i5513 wrote:
> Hello again, we have a problem with decimal numbers.
> When we have next loops, we don't get the result expected
> for ($i = 0.001; $i <= 0.01 ;$i += 0.001)

[...]


perldoc -q 9999


Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'


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

Date: 19 Dec 2002 10:43:39 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Strange loop
Message-Id: <ats7sr$5jh$1@mamenchi.zrz.TU-Berlin.DE>

According to i5513 <i5513@hotmail.com>:
> Hello again, we have a problem with decimal numbers.
> When we have next loops, we don't get the result expected
> for ($i = 0.001; $i <= 0.01 ;$i += 0.001)
> {
> 	print $i." ";
> }
> print "\n";
> # OUT: 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 WITHOUT
> 0.01!!!
> for ($i = 0.01; $i <= 0.1 ; $i += 0.01)
> {
> 	print $i." ";
> }
> print "\n";
> # OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1 ¡OK!
> for ($i = 0.01; $i < 0.1 ; $i += 0.01)
> {
> 	print $i." ";
> }
> # OUT: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 WITH 0.1!!!!!
> (look at ¡¡¡¡<!!!)
> 
> Where is the problem???? We don't know
> Thanks!

This happens because real arithmetic on computers is imprecise.  In
particular, the increments you're using (0.001, 0.01) don't have exact
representations as binary numbers, so an approximation is used.  After
ten summations, the result can be a (tiny) bit smaller or larger than
the theoretical value.  In the first case the loop runs one round more
than expected, because the condition "$i <= 0.1" is true one more time.

There are two standard methods to avoid this effect.  Either use integers
for loop control and derive the real value from that inside the loop:

    for ( 1 .. 10 ) {
        my $i = $_ * 0.01; # or 0.01, or whatever
        print "$i ";
    }

Alternatively, you can stick with real values in the loop control,
but then you must allow for some fuzz in the calculations:

    my $eps = 0.0000005;
    for (my $i = 0.01; abs( $i - 0.1) > $eps ; $i += 0.01) {
        print $i." ";
    }

In your case, I'd prefer the first solution.  The second one may be
appropriate for serious number crunching, but that is not one of
Perl's strengths.

Anno


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

Date: Thu, 19 Dec 2002 12:06:52 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Tracking User's OS type?
Message-Id: <Pine.LNX.4.40.0212191204430.5161-100000@lxplus076.cern.ch>

On Dec 19, Jürgen Exner inscribed on the eternal scroll:

> John wrote:
> > I recently created a website and I am able to do the following:
> >
> > Track IP Address, browser, etc.
> >
> > However, I can't seem to find how to track what OS the user is using.
> > Is there a simple line of perl code that will accomplish this?
>
> Please see "perldoc perlvar":
> $OSNAME
> $^O     The name of the operating system under which this copy of Perl
>         was built, as determined during the configuration process.

With respect, I presume that the hon Usenaut already knows what OS is
running on the server, and is more interested to know what the caller
is using.  It's unlikely that the caller will consent to running his
perl code on their client platform for this...?



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

Date: Thu, 19 Dec 2002 07:31:29 GMT
From: Leo <leo@jollibee.balei.org>
Subject: trapping connection timeout ... using sockets
Message-Id: <lBeM9.695$2j2.58949655@newssvr21.news.prodigy.com>

Is there a way to get out of connection timeout using IO::Socket,
if the host im trying to connect to is down or won't give you
any data for some time , how will I do this ... here's my function ...
thanks 

sub getdaytime {
        my $svrhost = shift ;
        my $socket = IO::Socket::INET->new("$svrhost:daytime") or die $! ;
        chomp(my $time = $socket->getline());
        return $time ;
}


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

Date: 19 Dec 2002 12:22:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: trapping connection timeout ... using sockets
Message-Id: <atsdlb$95b$1@mamenchi.zrz.TU-Berlin.DE>

According to Leo  <leo@jollibee.balei.org>:
> Is there a way to get out of connection timeout using IO::Socket,
> if the host im trying to connect to is down or won't give you
> any data for some time , how will I do this ... here's my function ...
> thanks 
> 
> sub getdaytime {
>         my $svrhost = shift ;
>         my $socket = IO::Socket::INET->new("$svrhost:daytime") or die $! ;
>         chomp(my $time = $socket->getline());
>         return $time ;
> }

See the FAQ: "perldoc -q 'slow event'".  This will point you to other
documentation, but it's a starting point.

Anno


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

Date: Thu, 19 Dec 2002 09:35:57 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Why do beginners read files into an array?
Message-Id: <3e01926d.1257938630@news.cis.dfn.de>

On Thu, 19 Dec 2002 08:29:28 +1100, "Tintin"
<me@privacy.net> wrote:

>> Keeping open links to system files as minimal or as short as 
>> possible (NFS!), does not seem silly to me.
>
>That might be so, but would only be the case in a *very* small 
>percentage of usage.

Almost every single file (about 99.9999%) I ever access
using Perl, except for trivial temporary examples, are
kept on NFS or NTFS mounted drives.  I almost never
slurp them into an array.  It is usually the worst way
to do it.
-- 
Regards, Helgi Briem
helgi AT decode DOT is

                           A: Top posting
                           Q: What is the most irritating thing on Usenet?
                                           - "Gordon" on apihna


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

Date: Thu, 19 Dec 2002 22:43:25 +1100
From: "Tintin" <me@privacy.net>
Subject: Re: Why do beginners read files into an array?
Message-Id: <atsbcs$25vnp$1@ID-172104.news.dfncis.de>


"Kevin Newman" <knewman00@yahoo.com> wrote in message
news:8ed70cf8.0212181703.5b3fc1b@posting.google.com...
> "Tintin" <me@privacy.net> wrote in message
news:<atpnn4$1cv63$2@ID-172104.news.dfncis.de>...
> > Can anyone come up with a plausible reason why so many beginners write
code
> > like:
> >
> > open FILE, "data.txt";
> > @var=<FILE>;
> > close FILE;
> >
> > foreach $line (@var) {
> >    print $line;
> > }
> >
> > Where does this logic come from?
> >
> > The only reason I can come up with is that the newbies are learning from
bad
> > examples.
>
> Speaking as a beginner, what's the "correct" way to do it?

There's no "correct" ways, just more appropriate ways depending on what you
want to do.

Suppose you are processing a 20GB file on a system with 512MB of memory and
your script wants to read a file and display lines matching a particular
pattern.

The more "appropriate" way of doing it is:

open FILE, "file.data" or die "Can not open file.data $!\n";

while (<FILE>)  {
   next unless (/some pattern/);
   print;
}

close FILE;

Now compare that to

open FILE, "file.data" or die "Can not open file.data $!\n";

@data = <FILE>;

foreach (@data) {
    next unless (/some pattern/);
    print;
}


BOOM!  There goes your memory!




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

Date: Thu, 19 Dec 2002 22:52:28 +1100
From: "Tintin" <me@privacy.net>
Subject: Re: Why is system call exit code factor of 256?
Message-Id: <atsbts$23elf$2@ID-172104.news.dfncis.de>


"Brandon Hoppe" <bhoppe@ti.com> wrote in message
news:atqbsm$btc$1@tilde.itg.ti.com...
>
> The value returned from a system() call to a perl program returns the exit
> value * 256. For example, if the program exited with exit(2), then
system()
> would return 512.
>
> Why is this?

Why did you multi-post instead of cross-post?

See http://www.cs.tut.fi/~jkorpela/usenet/xpost.html




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

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


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