[23416] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5634 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 8 11:06:10 2003

Date: Wed, 8 Oct 2003 08:05:10 -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           Wed, 8 Oct 2003     Volume: 10 Number: 5634

Today's topics:
    Re: //= operator alternative (Rafael Garcia-Suarez)
    Re: //= operator alternative (Rafael Garcia-Suarez)
    Re: About regura expression only for outside of html ta <mo@naggama.co.kr>
    Re: computer name and domain, change, win2k (Anno Siegel)
    Re: IsSorted @list ? 1 : 0 (Anno Siegel)
        Mail::POP3Client <Jeff@aetherweb.co.uk>
    Re: Mail::POP3Client <josef.moellers@fujitsu-siemens.com>
    Re: Newbie needs some help <a@b.com>
    Re: Newbie needs some help <HelgiBriem_1@hotmail.com>
    Re: Newbie needs some help <kjohnson@wvr.net>
    Re: Newbie needs some help <HelgiBriem_1@hotmail.com>
    Re: NEWBIE!  Please help! (Anno Siegel)
    Re: NEWBIE!  Please help! <tom@ztml.com>
    Re: NEWBIE!  Please help! (Tad McClellan)
    Re: Perl::Inline compiling problems <kalinaubears@iinet.net.au>
    Re: Reading huge *.txt files? <me@privacy.net>
    Re: regex behavior (Anno Siegel)
        rotate items in log file <robertw@nospam.acm.org>
    Re: rotate items in log file (Tad McClellan)
    Re: rotate items in log file <noreply@gunnar.cc>
        Send two documents to browser? - repost <REMOVEXtwoheadsX@tiscaliX.co.uk>
    Re: Send two documents to browser? - repost <nospam@bigpond.com>
    Re: Send two documents to browser? - repost <tom@ztml.com>
        Simple Regex Problem <rob@nospam.here>
    Re: Simple Regex Problem (Anno Siegel)
    Re: Simple Regex Problem (Lack Mr G M)
    Re: Simple Regex Problem (Tad McClellan)
        Sockets question <no@spam.here>
    Re: Teach me how to fish, regexp (Roy Johnson)
    Re: Teach me how to fish, regexp (Tad McClellan)
    Re:  <bwalton@rochester.rr.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 08 Oct 2003 08:07:06 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: //= operator alternative
Message-Id: <slrnbo7h1g.p81.rgarciasuarez@rafael.serd.lyon.hexaflux.loc>

Roy Johnson wrote:
>Having thought about it some more, I got a much better, more
>comprehensive idea.
>
>Change the behavior of defined() thus: it provides a context in which
>only undefined values are false. Boolean operators return undef rather
>than empty string.
>
>Then this would work exactly like you'd expect:
> if (defined($a or $b)) { ...

well, the problem with contexts is that you're going to face
awkward propagation problems like this one :
    if (defined(foo()) { ... }
    sub foo { $a or $b }
I understand that Perl 6 will have more contexts that Perl 5, so
maybe will it have a defined-boolean context in addition to the regular
boolean one. (I don't think so, though.)

-- 
Unprimed is not *NIX


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

Date: 08 Oct 2003 08:10:17 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: //= operator alternative
Message-Id: <slrnbo7h7g.p81.rgarciasuarez@rafael.serd.lyon.hexaflux.loc>

Roy Johnson wrote:
>//= will be forthcoming in perl6 to handle defaulting undefined values
>(vs. merely false ones). In the threads discussing it, there was some
>noise made about needing another operator for exists, and maybe a
>special alternative to the (ever so rarely used) &&=, etc.

// and //= will be present in perl 5.10.

>It occurred to me that maybe what ought to be done was to make tests
>act as assigners (or yield lvalues, sort of) in certain situations --
>in particular, in situations where ||= or &&= is specified, assign to
>the argument being tested:
>
>defined($var) ||= 'default';
>exists($hash{$key}) &&= 'replacement';
>
>To me, this reads pretty well. From a syntax standpoint, it's very
>irregular, but that hasn't stopped Perl in the past.

At first sight, I like this syntax. It think it could be implemented in
Perl 5 without too much pain. (Perl 5 language suggestions should be
directed at the perl 5 porters mailing list, by the way.)

-- 
Ungrateful is not *NIX


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

Date: Wed, 8 Oct 2003 20:50:46 +0900
From: "Mo/G&G" <mo@naggama.co.kr>
Subject: Re: About regura expression only for outside of html tags.
Message-Id: <bm0tiu$l6q$1@news.hananet.net>


It works perfect.
$aaa = preg_replace("/((<[^>]*)|\d)/e", '"\2"=="\1"? "\1":"0"', $sentence);

Thanks

Regards,
Mo



"G&G" <chang@naggama.co.kr> wrote in message
news:blu2f5$qnv$1@news.hananet.net...
> Have any idea about regular expression ?
>
>
> I tried to change any numeric letter to Zero like following;
> $aaa = ereg_replace ("|0-9|", "0", $sentence);
> This seems working perfect.
>
> My question is how I can do this function only for outside of html tags.
> I don't want any numeric letters to be changed inside html tags.
>
> Thanks for further response.
>
> Regards,
> Mo
>
>
>
>




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

Date: 8 Oct 2003 08:34:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: computer name and domain, change, win2k
Message-Id: <bm0i5r$a2j$1@mamenchi.zrz.TU-Berlin.DE>

marge dahl <mardahl2000@yahoo.com> wrote in comp.lang.perl.misc:
> Sitting at the w2k workstation.  Have a perl script that does
> many things - one thing I can't figure out though is how to
> rename the computer and domain.

First find out how to change the computer and domain names using your
system.  A Perl group can't help you with that.  (You may find that you
can't change the domain name.)  When you know how to do that, figure out
how to use Perl to do the same thing.  If you have problems at that
stage we may be able to help.

Anno


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

Date: 8 Oct 2003 13:06:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: IsSorted @list ? 1 : 0
Message-Id: <bm124i$hhi$2@mamenchi.zrz.TU-Berlin.DE>

Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> King <king21122@yahoo.com> writes:
> 
> > I need to find out if a data file is sorted by it's first column with
> > is suppose to be a unique value.
> > I am thinking
> > while (<FH>) {
> >    push @key, split [0];
> > }
> > IsSorted @key ? do this : do that;
> 
> I suspect you are asking if there is some cleaver short-cut to
> implement the IsSorted function.  
> 
> I think the answer is no.  You just do it the obvious way (compare
> each element with the next).

If you want the obvious way condensed in a subroutine, here is
how it could be written:

    sub is_sorted {
        $_[ $_] > $_[ $_ + 1] and return 0 for 0 .. $#_ - 1;
        1;
    }

Anno


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

Date: Wed, 8 Oct 2003 14:32:29 +0100
From: "Jeff Snoxell" <Jeff@aetherweb.co.uk>
Subject: Mail::POP3Client
Message-Id: <bm13k6$k79$1@news6.svr.pol.co.uk>

Hello,

I'm trying to develop a script to do some Spam filtering for me. I've got my
script running no problem. It connects to my pop box, scans and deletes
emails, logs its activity etc.

Problem is, once the scan is completed, ALL my emails are marked as having
been read, so when I subsequently download them with my mail client, they're
not flagged up correctly.

I've tried calling $client->unseen($i) for each message after I've finished
with it; but this doesn't seem to do anything.

Thanks for any help you can offer,

Jeff Snoxell
http://www.aetherweb.co.uk




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

Date: Wed, 08 Oct 2003 16:12:41 +0200
From: Josef =?iso-8859-1?Q?M=F6llers?= <josef.moellers@fujitsu-siemens.com>
Subject: Re: Mail::POP3Client
Message-Id: <3F841B59.C3A39CB0@fujitsu-siemens.com>

Jeff Snoxell wrote:
> =

> Hello,
> =

> I'm trying to develop a script to do some Spam filtering for me. I've g=
ot my
> script running no problem. It connects to my pop box, scans and deletes=

> emails, logs its activity etc.
> =

> Problem is, once the scan is completed, ALL my emails are marked as hav=
ing
> been read, so when I subsequently download them with my mail client, th=
ey're
> not flagged up correctly.
> =

> I've tried calling $client->unseen($i) for each message after I've fini=
shed
> with it; but this doesn't seem to do anything.

I'm doing the very same thing, but based on IMAP. There you can say
	$imap->Peek(1);
to prevent the setting of the /Seen flag in the first place.

HTH,

Josef
-- =

Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


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

Date: Wed, 08 Oct 2003 00:10:00 -0700
From: "someone@example.com" <a@b.com>
Subject: Re: Newbie needs some help
Message-Id: <pan.2003.10.08.07.10.00.68683@b.com>

On Wed, 08 Oct 2003 04:29:33 +0000, Kenneth D. Johnson wrote:

> 
> I want to find the lines in file2 that contain the numbers in file1 so I can
> write to a third file the A, B, C, D, E info that follows the numbers in
> file2.
> 
> I will sure appreciate any help.

If you want help, then you should show what you've tried and failed at
doing.  By supplying an answer, I am a screwing you out of the joy of
learning.  Sorry.

I'd start here:
http://learn.perl.org/library/beginning_perl/


use strict;
use warnings;

open FILE1, "file1" or die $!;
open FILE2, "file2" or die $!;
open FILE3, "> file3" or die $!;
my %valid;

while (<FILE1>)
{
  chomp;
  $valid{$_} = 1;
}

close FILE1 or die $!;

while (<FILE2>)
{
  if (/\[(\S+)\]\s+(.*)$/ and $valid{$1})
   { print FILE3 $2 . "\n" }
}

close FILE2 or die $!;
close FILE3 or die $!;




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

Date: Wed, 08 Oct 2003 09:53:16 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Newbie needs some help
Message-Id: <6cm7ovo1hpgrk8p1ck4rte3415c6th8eqa@4ax.com>

On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
<kjohnson@wvr.net> wrote:

>I have file1 that has:
>100
>200
>300
>etc.
>
>I have file2 that has
>XXX XX XXXXXX  XXXX  XXXX [100] AAA
>XXX XX XXXXXX  XXXX  XXXX [200] BBB
>XXX XX XXXXXX  XXXX  XXXX [225] CCC
>XXX XX XXXXXX  XXXX  XXXX [250] DDD
>XXX XX XXXXXX  XXXX  XXXX [300] EEE
>etc.
>
>I want to find the lines in file2 that contain the numbers in file1 so I can
>write to a third file the A, B, C, D, E info that follows the numbers in
>file2.

Here's one way:

#!perl
use warnings;
use strict;
my $usage = "Usage: $0 wanted infofile outfile\n";
die $usage if not $ARGV[2];
my ($wanted, $infofile, $outfile) = @ARGV;

my %wanted;
open WANTED, $wanted or die "Cannot open $wanted:$!\n";
while (<WANTED>)
{
  next if (/^\s+$/);  # ignore  blank lines
  chomp;
  $wanted{$_}++;
}
close WANTED;

open OUT, ">", $outfile 
    or die "Cannot open $outfile for writing:$!\n";

open INFO, $infofile or die "Cannot open $infofile:$!\n";
{
  while (<INFO>)
  {
     next if (/^\s+$/);  # ignore blank lines
   if (/\[(\d+)\](.+)/ and ($wanted{$1}))
   {
   print OUT $1,$2, "\n";
  }
  }
}
close INFO;
close OUT or die "Cannot close $outfile:$!\n";


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

Date: Wed, 08 Oct 2003 13:47:22 GMT
From: "Kenneth D. Johnson" <kjohnson@wvr.net>
Subject: Re: Newbie needs some help
Message-Id: <KzUgb.6945$qK1.5593916@news2.news.adelphia.net>

Thanks to the several who helped and even to those who only have this forum
as an opportunity to show their genius.  :)  One learns from all.

KDJ

-- 
"We cannot have real learning in school if we think it is our duty and our
right to tell children what they must learn."
John Holt
How Children Fail (179)

"Helgi Briem" <HelgiBriem_1@hotmail.com> wrote in message
news:6cm7ovo1hpgrk8p1ck4rte3415c6th8eqa@4ax.com...
> On Wed, 08 Oct 2003 04:29:33 GMT, "Kenneth D. Johnson"
> <kjohnson@wvr.net> wrote:
>
> >I have file1 that has:
> >100
> >200
> >300
> >etc.
> >
> >I have file2 that has
> >XXX XX XXXXXX  XXXX  XXXX [100] AAA
> >XXX XX XXXXXX  XXXX  XXXX [200] BBB
> >XXX XX XXXXXX  XXXX  XXXX [225] CCC
> >XXX XX XXXXXX  XXXX  XXXX [250] DDD
> >XXX XX XXXXXX  XXXX  XXXX [300] EEE
> >etc.
> >
> >I want to find the lines in file2 that contain the numbers in file1 so I
can
> >write to a third file the A, B, C, D, E info that follows the numbers in
> >file2.
>
> Here's one way:
>
> #!perl
> use warnings;
> use strict;
> my $usage = "Usage: $0 wanted infofile outfile\n";
> die $usage if not $ARGV[2];
> my ($wanted, $infofile, $outfile) = @ARGV;
>
> my %wanted;
> open WANTED, $wanted or die "Cannot open $wanted:$!\n";
> while (<WANTED>)
> {
>   next if (/^\s+$/);  # ignore  blank lines
>   chomp;
>   $wanted{$_}++;
> }
> close WANTED;
>
> open OUT, ">", $outfile
>     or die "Cannot open $outfile for writing:$!\n";
>
> open INFO, $infofile or die "Cannot open $infofile:$!\n";
> {
>   while (<INFO>)
>   {
>      next if (/^\s+$/);  # ignore blank lines
>    if (/\[(\d+)\](.+)/ and ($wanted{$1}))
>    {
>    print OUT $1,$2, "\n";
>   }
>   }
> }
> close INFO;
> close OUT or die "Cannot close $outfile:$!\n";




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

Date: Wed, 08 Oct 2003 14:20:49 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Newbie needs some help
Message-Id: <e678ovojs50hqga0smptqi26nj5qdo3m9e@4ax.com>

On Wed, 08 Oct 2003 13:47:22 GMT, "Kenneth D. Johnson"
<kjohnson@wvr.net> wrote:

>Thanks to the several who helped and even to those who only have this forum
>as an opportunity to show their genius.  :)  One learns from all.

You thank me and then spit in my face by rudely top-posting
with a full copy.  Don't do that.  If you don't know what top-posting
is, read:

http://www.catb.org/~esr/jargon/html/T/top-post.html

For further guidelines on posting to comp.lang.perl.misc read:

http://mail.augustmail.com/~tadmc/clpmisc.shtml

 For more information about netiquette in general, see the 
"Netiquette Guidelines" at:

 http://andrew2.andrew.cmu.edu/rfc/rfc1855.html




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

Date: 8 Oct 2003 09:21:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: NEWBIE!  Please help!
Message-Id: <bm0kuk$a2j$2@mamenchi.zrz.TU-Berlin.DE>

tom  <tom@ztml.com> wrote in comp.lang.perl.misc:
> bdknoll - runbox.com (BDK) wrote:
> >I am trying to take a peice of a word and replace it with output of
> >the chr function.  So the word is ki45.  I want to replace the 45 with
> >what "print (chr(36));" returns.
> >
> >Here is what I have tried:
> >
> >$word = ki45;
> >$word =~ s/(45)/(print (chr(chr36))/;
> .
> Use sprintf for the above:
> 
> $word =~ s/(45)/sprintf ("%s",chr(36))/e;

Why?  How does "sprintf()" improve the value of "chr(36)"?

Anno


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

Date: Wed, 08 Oct 2003 11:29:09 GMT
From: tom <tom@ztml.com>
Subject: Re: NEWBIE!  Please help!
Message-Id: <9ySgb.36426$541.2882@nwrdny02.gnilink.net>

anno4000 - lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>tom  <tom@ztml.com> wrote in comp.lang.perl.misc:
>> bdknoll - runbox.com (BDK) wrote:
>> >I am trying to take a peice of a word and replace it with output of
>> >the chr function.  So the word is ki45.  I want to replace the 45 with
>> >what "print (chr(36));" returns.
>> >
>> >Here is what I have tried:
>> >
>> >$word = ki45;
>> >$word =~ s/(45)/(print (chr(chr36))/;
>> .
>> Use sprintf for the above:
>> 
>> $word =~ s/(45)/sprintf ("%s",chr(36))/e;
>
>Why?  How does "sprintf()" improve the value of "chr(36)"?
>
>Anno
>

It would not on the example. I was simply pointing out that the function 
sprintf should be used instead of print - print returns true or false whereas 
sprintf returns the value, which that was what the example need.

Tom
ztml.com


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

Date: Wed, 8 Oct 2003 08:06:07 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: NEWBIE!  Please help!
Message-Id: <slrnbo82tv.lcb.tadmc@magna.augustmail.com>

tom <tom@ztml.com> wrote:
> anno4000 - lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>>tom  <tom@ztml.com> wrote in comp.lang.perl.misc:
>>> bdknoll - runbox.com (BDK) wrote:

>>> >$word =~ s/(45)/(print (chr(chr36))/;
>>> .
>>> Use sprintf for the above:
>>> 
>>> $word =~ s/(45)/sprintf ("%s",chr(36))/e;
>>
>>Why?  How does "sprintf()" improve the value of "chr(36)"?

> It would not on the example. I was simply pointing out that the function 
> sprintf should be used instead of print - print returns true or false whereas 
> sprintf returns the value, which that was what the example need.


chr() returns the value too, so there is no need to pass it
through any other function first.


   $word =~ s/(45)/ chr(36) /e;


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


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

Date: Wed, 08 Oct 2003 17:19:53 +1000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: Perl::Inline compiling problems
Message-Id: <3f83bb52$0$23608$5a62ac22@freenews.iinet.net.au>

Sisyphus wrote:

> 
> I've always had to use the Stack to pass arrays to Inline C. (See 
> 'perldoc Inline::C-Cookbook' on how to pass varying size argument 
> lists.) Afaik it wasn't until 5.8 that you could pass AV*s, courtesy of 
> a fixed typemap. But if you're experiencing no problem .... maybe you've 
> got a 5.6 with a fixed typemap.
> 
> (I'll check my facts, too - and report back if I've got it wrong.)
> 

I tried the following (transcribed) code:

use Inline C => <<'EOC';
SV ** elem;
int ret;

int dumb(AV* arref) {
     elem = av_fetch(arref, 2, 0);
     ret = SvIV(*elem);
     return ret;
     }

EOC

print dumb([7..15]); #line 16

On 5.6.1 I get:
'arref is not of type AVPtr at try.pl line 16'.

On 5.8.0 I get (the correct response):
9

I've also seen it stated in 'Extending and Embedding Perl' by Jenness 
and Cousins (footnote on page 243) that you can't pass AV*s to Inline 
with pre-5.8 versions of perl.

Interesting that it's not posing any problems for you - and half your 
luck :-)

Incidentally, I've not detected any speed advantage using an AV* instead 
of the stack - but far be it for me to say whether that's simply because 
I haven't looked properly, or whether it's because there's little 
difference in efficiency between the 2 methods :-)


-- 
To reply by email u have to take out the u in kalinaubears.



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

Date: Wed, 8 Oct 2003 23:18:04 +1300
From: "Tintin" <me@privacy.net>
Subject: Re: Reading huge *.txt files?
Message-Id: <bm0oa8$hktoi$1@ID-172104.news.uni-berlin.de>


"Jesper" <hobbes@vkr.NOSPAM.dk> wrote in message
news:bluigq$gap$1@news.net.uni-c.dk...
> I have the same problem.
> I have two txt files (txta and texb) each with 15-20000 lines. Each line
> contains a username + info. Now what I want to do is take a line from
texta,
> retrieve the username from this line, search for the username in txtb and
> return the info that's related to the username i txtb. My code looks like:
>
> open (txta, "txta");
> @txta= <txta>;
> close(txta);
>
> open (txtb, "txtb");
> @txtb= <txtb>;
> close(txtb);

[snipped rest of code]

I'm don't want to critical of your code (Eric did a good, but justified job
of that).

What I really want to know (and there was a long thread about this not too
long ago), is what thought processes led you to think that slurping a large
file into an array was a good thing to do (for this particular case)?




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

Date: 8 Oct 2003 11:53:03 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: regex behavior
Message-Id: <bm0tqv$hhi$1@mamenchi.zrz.TU-Berlin.DE>

Michael P. Broida <michael.p.broida@boeing_oops.com> wrote in comp.lang.perl.misc:
> Jeff 'japhy' Pinyan wrote:
> > 
> > On Wed, 1 Oct 2003, Michael P. Broida wrote:
> > 
> > >       Now why isn't the () part of that getting the FIRST of those
> > >       characters?
> > 
> > It does... but then the + modifier causes $1 to be repopulated with the
> > NEXT character \w matches, and so on.
> 
> 	(I e-mailed a different response, then thought about it more.)
> 
> 	Hmm, that explains it pretty well.  I guess my only remaining
> 	question would be:  why does it actually "repopulate"??
> 
> 	It seems as though, once it matches that single character, it
> 	would/should save it in $1 as the () directs, and the NEXT
> 	matched character would go into $2 instead of being thrown
> 	away, and the next in $3, etc.  I mean, the + seems to be
> 	telling it to repeat the entire (\w) operation, and THAT
> 	is saving characters.

Yes, but it only has *one* $n variable to save to, determined by the number
of the opening parenthesis of the capturing pair.  It isn't free to use
more $n variables for additional matches, because those may be occupied
by other capturing pairs.

So there's hardly a choice but to overwrite what's already there.

Anno


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

Date: Wed, 08 Oct 2003 09:30:26 -0400
From: Robert Wallace <robertw@nospam.acm.org>
Subject: rotate items in log file
Message-Id: <3F841172.C9DB2654@nospam.acm.org>

in my code i'm trying to keep a log file of say 10 lines.
if a line is added and the log file is more than 10 lines, then the last
line is removed before a new line is added.
if the log file is less than 10 lines, then the new line will be added
and no lines will be removed.
here's my first attempt:


   #append and clean logfile
    my(@logFile);
    open (READ, $logFile);
    chomp(@logFile=<READ>);
    close (READ);
    my($size)=@logFile;
    if ($size > 10){
	unshift @logFile, $search;
	pop @logFile;
    }

    my($logFileWrite)=join "\n", @logFile;
    open (WRITE, ">$logFile");
    print WRITE $logFileWrite;
    close (WRITE);

the problem with unshift/pop here is that if there's only one line it'll
still rotate.
so I added the if statement. but if there are say 19 lines, it'll rotate
the 19 lines instead of trimming off 9.

is there a way perhaps to move 10 items from one array to another
like psudo code:
@arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
rest;


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

Date: Wed, 8 Oct 2003 09:29:04 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: rotate items in log file
Message-Id: <slrnbo87pg.lij.tadmc@magna.augustmail.com>

Robert Wallace <robertw@nospam.acm.org> wrote:

> in my code i'm trying to keep a log file of say 10 lines.


Why don't you just use an existing wheel, such as Logfile::Rotate ?


> if a line is added and the log file is more than 10 lines, then the last
> line is removed before a new line is added.

>     open (READ, $logFile);


You should always, yes *always*, check the return value from open():

   open (READ, $logFile) or die "could not open '$logFile'  $!";


>     chomp(@logFile=<READ>);
>     close (READ);
>     my($size)=@logFile;


That does NOT get the size of the @logFile array, that gets the 
1st element, same as:

   my($size) = $logFile[0];

You need the name of the array in _scalar_ context if you want
to get the size:

   my $size = @logFile;


>     if ($size > 10){


Here you already have a scalar context, so you don't even need the $size
temporary variable:

   if (@logFile > 10) {


>     my($logFileWrite)=join "\n", @logFile;


Why did you chomp() them above only to put the newlines back in?

(and why no newline on the last element?)


I don't see any file locking going on. Is logging going on while
you are rotating the log?


> is there a way perhaps to move 10 items from one array to another


You can also do it from one array back to the _same_ array.


> like psudo code:
> @arrayClean=@array [item 1], [item 2] ... [item 10]...throw out the
> rest;


Yes, by using an "array slice":

   @array = @array[ 0 .. 9 ];

or, in keeping with your write-only style:

   @array=@array[0..9];


Space characters are not a scarce resource, feel free to use as many
as you like to make your code easier to read and understand.


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


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

Date: Wed, 08 Oct 2003 16:45:24 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: rotate items in log file
Message-Id: <bm1802$h9tg7$1@ID-184292.news.uni-berlin.de>

Robert Wallace wrote:
> in my code i'm trying to keep a log file of say 10 lines. if a line
> is added and the log file is more than 10 lines, then the last line
> is removed before a new line is added. if the log file is less than
> 10 lines, then the new line will be added and no lines will be
> removed. here's my first attempt:
> 
>     my($size)=@logFile;
--------^-----^
Remove those parentheses, or else $size is assigned the first line 
rather than the number of lines.

>     if ($size > 10){
> 	unshift @logFile, $search;
> 	pop @logFile;
>     }

Try this instead:

     unshift @logFile, $search;
     splice @logFile, 10 if $size >= 10;

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



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

Date: Wed, 08 Oct 2003 09:56:59 +0100
From: Mark <REMOVEXtwoheadsX@tiscaliX.co.uk>
Subject: Send two documents to browser? - repost
Message-Id: <45j7ovkt9uhjjpbs676sjr0s2suuqlg5g5@4ax.com>

Hi All, 

This is a repost of an earlier question as I don't think I made the
problem explicit enough, or highlighted the dependence on a perl based
solution.

I have a commercial perl search engine script that is slow in
producing results. As a consequence, once the user presses the search
button he/she has to stare at a glaring white blank screen until the
results are returned. I would like the script to send a temporary
document to the browser until the search is complete and then replace
this document with the search results. 

It has been suggested in earlier replies that I use an interstitial
page and browser redirects to achieve this but I don't see how this
will work as if the script returns a temporary page which then
redirects, where will it redirect to? If it calls the script again
then we are no further forward. 

The search engine is part of a complex shopping basket and we have
neither the expertise or the budget to make major changes to the
application. What I am hoping to find is a perl solution that I can
insert at the start of the search script that will send out the
temporary page while still allowing the script to do the searches and
then replace the temporary page with its results. We thought we had
solved the problem but instead of the search results replacing the
temporary page they were added below the temporary page, causing
essential header information to be printed rather than being
interpreted by the browser. 

I have heard that perl TK is able to return yes/no pop ups whilst
still allowing the script to run in the background. Does anyone know
if perl TK could produce the required temporary document?

Is there any way I could fork to a process that would send the
temporary document yet still allow the search script to replace this
document once it is completed? 

Many thanks for your interest, any information is gratefully received.

Mark


The previious post.
> Hi All, 
> 
> I have a commercial website search script which is slow in returning
> its results. The consequence is that for a short period the screen is
> white until the results are returned. 
> 
> Is there a simple way to make the script return a temporary document
> to the browser which is then replaced by the search results? 
> 

Yes, there are simple ways to do this. However, they are no more 
Perl-specific than they are Cobol-specific. Rather than give you a
whole 
solution here (and encourage more non-Perl-specific questions,
drowning 
out those with Perl questions), I'll mention "interstitial pages", 
"browser redirects", and point you toward Google and the 
comp.infosystems.www.authoring.cgi newsgroup.


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

Date: Wed, 08 Oct 2003 19:09:23 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Send two documents to browser? - repost
Message-Id: <1474228.84LczSU61S@gregs-web-hosting-and-pickle-farming>

It was a dark and stormy night, and Mark managed to scribble:

> Hi All,
> 
> This is a repost of an earlier question as I don't think I made the
> problem explicit enough, or highlighted the dependence on a perl based
> solution.
> 
> I have a commercial perl search engine script that is slow in
> producing results. As a consequence, once the user presses the search
> button he/she has to stare at a glaring white blank screen until the
> results are returned. I would like the script to send a temporary
> document to the browser until the search is complete and then replace
> this document with the search results.
> 
> It has been suggested in earlier replies that I use an interstitial
> page and browser redirects to achieve this but I don't see how this
> will work as if the script returns a temporary page which then
> redirects, where will it redirect to? If it calls the script again
> then we are no further forward.
> 

vbulletin does this sort of thing. Its written in PHP and you can download a demo version http://www.vbulletin.com/

When you do a search, it displays a page saying that is doing the search, then displays whe results. Have a look at the HTML code that it generates.

gtoomey


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

Date: Wed, 08 Oct 2003 14:37:39 GMT
From: tom <tom@ztml.com>
Subject: Re: Send two documents to browser? - repost
Message-Id: <TiVgb.36469$541.24624@nwrdny02.gnilink.net>

Mark (tiscaliX.co.uk) wrote:
>Hi All, 
>
>This is a repost of an earlier question as I don't think I made the
>problem explicit enough, or highlighted the dependence on a perl based
>solution.
>
>I have a commercial perl search engine script that is slow in
>producing results. As a consequence, once the user presses the search
>button he/she has to stare at a glaring white blank screen until the
>results are returned. I would like the script to send a temporary
>document to the browser until the search is complete and then replace
>this document with the search results. 
>

You might want to use Javascript to do that. For example, the following script 
mycgiscript.pl will generate an input form for the search, when the form is 
submitted it will create a popup window telling the user that the search is in 
progress - meanwhile your script can continue process the search results, when 
done the popup window will close.

#!/usr/bin/perl

use strict;

### process your search results when the form is submitted
if(@ARGV)
{
	print <<HTML;
Content-Type: text/html

<html><head><title>Search results</title></head>
<body><h3>Search completed...</h3>
Here are the results...

HTML
	sleep(10); # show your search result here

	exit;
}

### generate your search form here
print <<HTML;
Content-Type: text/html

<html><head><title>Popup window</title></head>
<body onUnload='javascript:msg.window.close()'>
<script language='JavaScript'>
function warning()
{
	msg = window.open("","msg","width=600,height=150");
	msg.document.open("text/html");
	msg.document.write("<br><br><h2 align=center><font color=red>Searching 
in progress...</font><h2>");
	return true;
}
var msg;
</script>

<h3>Search for...</h3>

<form method="post" action="mycgiscript.pl?testing" onSubmit='return warning
()'>
<table>
<tr><th>Search for</th><td><input /></td>
<tr><td><input value="SEARCH" type="submit"  /></td>
</tr></table></form></body></html>

HTML

### end of mycgiscript.pl

 ... or if you do not want to use Javascript, you might want to try this:

#!/usr/bin/perl

use strict;

print "Content-Type: text/html

<html><head><title>Time intensive task</title><head><body>
<h3><font size=6 color=red>This task will take a long long time...</font></h3>
Go have a cup of coffee while you're waiting.<p>
<h3>Please do not close or use this window while the task is in 
progress.</h3>";
for my $ix(0..4000) { print " " } # fill the browser's buffer so that it will 
be flushed
#
#
	sleep(10); # do your processing...
#
#
print "<h2>DONE!</h2>Now you may use this window again.";

The above script will simply force the browser to flush its buffer so that the 
user will not need to look at a blank screen.

Tom
ztml.com



 


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

Date: Wed, 08 Oct 2003 11:39:37 +0100
From: Robert <rob@nospam.here>
Subject: Simple Regex Problem
Message-Id: <ibp7ov0qcuq7quoq6jtugamd177tdhgvdh@4ax.com>

I am struggling with the following (simple) problem.
Given a list of vehicles I need to capitalise the first letter of the
make and model and change the spec to uppercase.

eg.
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX

The closest I can get is this but it capitalises every word.

$model =~ s/(\w+)/\u\L$1/g;

Any pointers would be appreciated.



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

Date: 8 Oct 2003 11:03:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Simple Regex Problem
Message-Id: <bm0qua$ga8$1@mamenchi.zrz.TU-Berlin.DE>

Robert  <rob@nospam.here> wrote in comp.lang.perl.misc:
> I am struggling with the following (simple) problem.

How do you know it's simple if you're still struggling?

> Given a list of vehicles I need to capitalise the first letter of the
> make and model and change the spec to uppercase.
> 
> eg.
> ford mondeo 2.0 glx
> Ford Mondeo 2.0 GLX

How do you recognize which part is make, model, and spec?  I'll assume
it's always the first, second, and fourth of blank-separated words.

> The closest I can get is this but it capitalises every word.
> 
> $model =~ s/(\w+)/\u\L$1/g;

Well, what did you expect?  s///g treats all matches alike (/g stands
for global), so the result is the same for all.  You must treat the
parts separately.  Here is one way:

    $_ = 'ford mondeo 2.0 glx';
    my @parts = split;
    $_ = ucfirst( $_) for @parts[ 0, 1];
    $_ = uc( $_) for $parts[ 3];
    # less consistent, but more conventional: $parts[ 3] = uc $parts[ 3];
    my $trans = join ' ', @parts;
    print "$_ -> $trans\n";

Anno


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

Date: Wed, 08 Oct 2003 12:19:31 BST
From: gml4410@ggr.co.uk (Lack Mr G M)
Subject: Re: Simple Regex Problem
Message-Id: <2003Oct8.121931@ukwit01>

In article <ibp7ov0qcuq7quoq6jtugamd177tdhgvdh@4ax.com>, Robert <rob@nospam.here> writes:
|>
|> Given a list of vehicles I need to capitalise the first letter of the
|> make and model and change the spec to uppercase.

   Assuming you *always* have a make and model and these are single words.

perl -lpe 's/^(.+?)\s(.+?)\s(.*)/\u\L$1\E \u\L$2\E \U$3/'
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX
FORD MONDEO 2.0 glx
Ford Mondeo 2.0 GLX


-- 
--------- Gordon Lack --------------- gml4410@ggr.co.uk  ------------
This message *may* reflect my personal opinion.  It is *not* intended
to reflect those of my employer, or anyone else.


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

Date: Wed, 8 Oct 2003 07:59:23 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Simple Regex Problem
Message-Id: <slrnbo82hb.lcb.tadmc@magna.augustmail.com>

Robert <rob@nospam.here> wrote:

> Given a list of vehicles I need to capitalise the first letter of the
> make and model and change the spec to uppercase.


How can you tell which part is "the spec" ?

I'll assume it is always the last thing.


> eg.
> ford mondeo 2.0 glx
> Ford Mondeo 2.0 GLX
> 
> The closest I can get is this but it capitalises every word.
> 
> $model =~ s/(\w+)/\u\L$1/g;


   $model =~ s/(\w+)$/\U$1/g;  # uc "the spec"


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


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

Date: Wed, 08 Oct 2003 12:57:30 GMT
From: "John" <no@spam.here>
Subject: Sockets question
Message-Id: <_QTgb.141720$bo1.91274@news-server.bigpond.net.au>

Hi,

One of the exercises in front of me is:
"Write a server in perl that will send the client a sleepy string of
"ZZZZZZZZZ" whenever it connects using port 54321 from the same host using a
socket in the UNIX domain."

Can someone possibly re-phrase the question? I have no idea about sockets -
just found a chapter on it in Prog. Perl but have not read it yet :(.
Any links out there I can get ideas/learn from?

Thanks in advance.




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

Date: 8 Oct 2003 07:06:41 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Teach me how to fish, regexp
Message-Id: <3ee08638.0310080606.5a95dbae@posting.google.com>

For learning how to fish, I recommend the book "Learning Perl". Randal
has a gift for introducing topics so that they are understandable and
stick with you.

As a versatile fishing pole you should already have, I direct you to
perldoc, and have included some baited hooks below. :-) Having a copy
of "Programming Perl" is a good alternative to looking everything up
in perldoc and man pages.


Henry <henryn@zzzspacebbs.com> wrote in message news:<BBA85E37.1558D%henryn@zzzspacebbs.com>...
> #1 What does using "my" mean? (Give me a clue -- a keyword; I'll look it up.
> Googling for "my" and "perl" has not been particularly enlightening.)

"my" declares a variable that exists only in the enclosing block. It
is "lexically scoped". When declaring variables, this is usually what
you want. Undeclared variables are global, and those declared "local"
are "dynamically scoped", meaning their values will persist into
function calls.

perldoc -f my
perldoc -f local

> #2 Is there any difference --except brevity-- between writing
>     \n{3}
> and
>      \n\n\n
> -- both mean "exactly 3 newlines in succession" right?

Correct. I like shorter expressions when possible, and prefer not to
have to count how many times I've put something.

> I understand the fundamental match expression components, excepting your use
> of parens and "?:".

(?:...) indicates that the enclosing parentheses will not be returned
by the match or assigned to $1, etc. They are only there to group the
pattern so that a quantifier can apply to it.

perldoc perlretut
perldoc perlre

> #3 To get started, is there a difference between uttering
>   my @paras = split(/\n{3}(\d+\.(?:\d+.)?)  /, $whole_file);
> and
>   my @paras = split /\n{3}(\d+\.(?:\d+.)?)   /, $whole_file;
> Both seem to work the same way in my test scaffolding.

Apart from the extra space in the 2nd pattern, they should be the
same. The parentheses for function calls are generally optional,
unless it becomes ambiguous which function gets an argument.

perldoc -f split
 
> #4 .... And I _really_ don't see how this expression leads to getting
> alternating saved section numbers and section contents in the output array.

It's how split works. The pattern you give it is the delimiter, which
is not ordinarily returned, but if you have parenthesized
sub-expression(s) in its pattern, they will be returned in order
amidst the rest of the data. (Except, of course, if you ?: the ()s.)

> > [...]I recommend \d instead of [0-9]).
> 
> OK, these are equivalent, though, right?  Is this a matter of style,
> dialect, common usage, modernity, or what?

Convenience, mostly. They are equivalent. I probably shouldn't have
thrown extras like that at you when you're trying to nail down more
fundamental stuff.

Read and practice.


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

Date: Wed, 8 Oct 2003 09:44:20 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Teach me how to fish, regexp
Message-Id: <slrnbo88m4.lk2.tadmc@magna.augustmail.com>

Roy Johnson <rjohnson@shell.com> wrote:

[snip good advice]

> "Programming Perl" is a good alternative to looking everything up
> in perldoc and man pages.


No it isn't.

The Camel book describes some old version of Perl.

The std docs describe the version of Perl that you 
actually have installed on your system.


"Programming Perl" is a good _supplement_ to looking everything 
up in perldoc.


[snip more good advice]


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


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

Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: 
Message-Id: <3F18A600.3040306@rochester.rr.com>

Ron wrote:

> Tried this code get a server 500 error.
> 
> Anyone know what's wrong with it?
> 
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {

(---^


>     dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
 ...
> Ron

 ...
-- 
Bob Walton



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

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


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