[29014] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 258 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 23 16:12:45 2007

Date: Fri, 23 Mar 2007 13:11:55 -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           Fri, 23 Mar 2007     Volume: 11 Number: 258

Today's topics:
    Re: First line in body of Mail::POP3Client <jgibson@mail.arc.nasa.gov>
    Re: How can I make my prime number generator better? anno4000@radom.zrz.tu-berlin.de
    Re: How can I make my prime number generator better? <abigail@abigail.be>
    Re: How can I make my prime number generator better? (Marc Espie)
    Re: How can I make my prime number generator better? <bugbear@trim_papermule.co.uk_trim>
    Re: How can I make my prime number generator better? anno4000@radom.zrz.tu-berlin.de
    Re: how to use perl to redirect web page? <glex_no-spam@qwest-spam-no.invalid>
    Re: I dotn understand this error <hjp-usenet2@hjp.at>
    Re: On Java's Interface (the meaning of interface in co <jwkenne@attglobal.net>
    Re: perl: adding lines and replacing stings <hjp-usenet2@hjp.at>
    Re: Problem with indirect variables <uri@stemsystems.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 23 Mar 2007 10:03:52 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: First line in body of Mail::POP3Client
Message-Id: <230320071003527094%jgibson@mail.arc.nasa.gov>

In article <1174657322.370768.134040@o5g2000hsb.googlegroups.com>,
<eng.john84@gmail.com> wrote:

>  How to write this code     if ( FIRST LINE IN BODY TRUE FOR EXAMPLE
> HELLO)

I am not sure what you mean by "FIRST LINE IN BODY TRUE FOR EXAMPLE
HELLO". A line is not true or false, although what it contains may be
interpreted as indicating trueness or falseness. Do you mean that the
line contains the string "HELLO" and nonthing else? See below.

>           {
> 
>            print $_, "\n"; } please help me to wrile this code
> 
> 
> code:
> 
> 
> #!/usr/local/bin/perl
>

Need here:

  use strict;
  use warnings;

> 
>   use Mail::POP3Client;
> 
> 
>  $pop = new Mail::POP3Client(   USER     => "xxxxxxxxx",
>                                PASSWORD => "xxxxxxx",
>                                HOST     => "xxxxx" );
> 
> 
>   {
>     foreach ( $pop->Body( 1 ) ) {
> 
> 
>     if ( FIRST LINE IN BODY TRUE FOR EXAMPLE HELLO)
>           {
> 
> 
>            print $_, "\n";
> 
> 
>            }}}
> 

The following will print any line of message 1 that contains HELLO
anywhere (untested):

  foreach ( $pop->Body(1) ) {
    print "$_\n" if /HELLO/;
  }

If you want the line to contain only HELLO and nothing else, then
change that to:

  foreach ( $pop->Body(1) ) {
    print "$_\n" if /^HELLO$/;
  }

or use a string equality for efficiency:

  foreach ( $pop->Body(1) ) {
    print "$_\n" if $_ eq 'HELLO';
  }

although in this case the line may contain a newline at the end and you
will need to either use chomp to remove or modify the test string to
"HELLO\n".

In most cases, you are better off using an explicit variable rather
than the default variable $_:

  foreach my $line ( $pop->Body(1) ) {
    print "$line\n" if $line eq 'HELLO';
  }

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: 23 Mar 2007 15:28:07 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: How can I make my prime number generator better?
Message-Id: <56ia07F28je93U1@mid.dfncis.de>

Michele Dondi  <bik.mido@tiscalinet.it> wrote in comp.lang.perl.misc:
> On Fri, 23 Mar 2007 12:39:38 +0000 (UTC), espie@lain.home (Marc Espie)
> wrote:
> 
> >Get rid of floating point arithmetic. Instead of time-consuming operation,
> >like looping from 1 to sqrt(n), write a while loop that stops when
> >i*i >= n.
> 
> sqrt(n) can be precalculated, i*i must be calculated for every loop.

Not necessarily.  If the algorithm involves the calculation of n/i
to determine divisibility, you can equivalently check for i >= n/i
at no extra cost.

Anno


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

Date: 23 Mar 2007 15:32:10 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: How can I make my prime number generator better?
Message-Id: <slrnf07smt.k6j.abigail@alexandra.abigail.be>

nikolas.britton@gmail.com (nikolas.britton@gmail.com) wrote on MMMMCMLII
September MCMXCIII in <URL:news:1174610848.316977.94170@y80g2000hsf.googlegroups.com>:
@@  How can I improve my code?... faster, better style, proper programming
@@  techniques, better algorithm? Thanks!

A better algorith beats everything else.

If you want to generate prime numbers starting from 1, sieves are your
best option. They have been your best option since antiquity.

Here's an example of a sieve:


#!/opt/perl/bin/perl -l

# A sieve based on base 6. Only possible primes are those numbers p
# for which p mod 6 == 1, or p mod 6 == 5.
#
# Let n = 6 * k + l (0 <= l < 6). Then the bit b associated with n is
# b = 2 * k + [undef, 0, undef, undef, undef, 1] -> [l]
# In reverse, given a bit b, the corresponding number n is
# n = 6 * int (b / 2) + [1, 5] -> [b % 2].

use strict;
use warnings 'all';

sub scrub    ($);
sub findnext ($);

my $max   = $ARGV [0] || 1000;

my @PRIMES    = (2, 3);
my $BASE      =  6;     # LCM of @PRIMES.
my $BITS_BASE =  2;
my $BITS_BYTE =  8;

my $bits  = $BITS_BASE * int ($max / $BASE) + ($max % $BASE ? $BITS_BASE : 0);
my $bytes = int ($bits / $BITS_BYTE) + ($bits % $BITS_BYTE ? 1 : 0);
my $sieve = eval qq !"\xFF" x $bytes!;  # Half the memory usuage.

# Numbers less than the base are special.
foreach my $prime (@PRIMES) {
    print $prime
}

my $i = 5;

for (; $i && $i <= sqrt ($max); $i = findnext $i) {
    print $i;
    scrub $i;
}

for (; $i && $i <        $max ; $i = findnext $i) {
    print $i;
}

exit;




my ($offset1, $offset2);
INIT {
    $offset1 = [undef, 0, undef, undef, undef, 1];
    $offset2 = [1, 5];
}

# Scrub out all multiples of the given argument. It's easy to see that
# all multiples less than the square already have been crossed out, and
# we only have to scrub out the odd multiples.
sub scrub ($) {
    use integer;
    my $n = shift;
    my $m = $n;
    if ($m % 3 == 1) {
        my $c = $n * $m;
        vec ($sieve, $BITS_BASE * ($c / $BASE) + $offset1 -> [$c % $BASE],
                     1) = 0;
        $m += 4;
    }

    my $c     = $n * $m;

    # $b is the bit index in the sieve, $b1 and $b2 are the increments
    # in the loop.
    my $b     = $BITS_BASE * ($c / $BASE) + $offset1 -> [$c % $BASE];
    my $b1    = (2 * $n / ($BASE / $BITS_BASE));
    my $b2    = (4 * $n / ($BASE / $BITS_BASE));

    # Exactly one of $b1, $b2 needs to be incremented by 1, 
    # depending on the parity of $c.
   ($c % 6 == 1 ? $b2 : $b1) += 1;

    # Make sure we don't go out of range.
    my $l     = $bits - $b1 - $b2;

    # Core loop, we want to minimize work here.
    while ($b <= $l) {
        vec ($sieve, $b, 1) = 0; $b += $b1;
        vec ($sieve, $b, 1) = 0; $b += $b2;
    }

    # Basically a copy of the previous loop, but with extra checks.
    # Checks don't hurt now as this loop is performed only once.
    {
        last if $b > $bits;
        vec ($sieve, $b, 1) = 0; $b += $b1;
        last if $b > $bits;
        vec ($sieve, $b, 1) = 0; $b += $b2;
    } 
}

# Find the next prime number after the given number.
sub findnext ($) {
    use integer;
    my $n = shift;
    my $i = $BITS_BASE * ($n / $BASE) + $offset1 -> [$n % $BASE] + 1;
    while ($i <= $bits) {
        if (vec ($sieve, $i, 1)) {
            return $BASE * ($i / $BITS_BASE) + $offset2 -> [$i % $BITS_BASE]
        }
        $i ++
    } undef;
}


__END__
-- 
print 74.117.115.116.32, 97.110.111.116.104.101.114.32,
      80.101.114.108.32, 72.97.99.107.101.114.10;


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

Date: Fri, 23 Mar 2007 16:24:21 +0000 (UTC)
From: espie@lain.home (Marc Espie)
Subject: Re: How can I make my prime number generator better?
Message-Id: <eu0uvl$2rvf$1@biggoron.nerim.net>

In article <o2r703himplfh1pmee1hfiq6mbto2fqjnj@4ax.com>,
Michele Dondi  <bik.mido@tiscalinet.it> wrote:
>On Fri, 23 Mar 2007 12:39:38 +0000 (UTC), espie@lain.home (Marc Espie)
>wrote:
>
>>Get rid of floating point arithmetic. Instead of time-consuming operation,
>>like looping from 1 to sqrt(n), write a while loop that stops when
>>i*i >= n.
>
>sqrt(n) can be precalculated, i*i must be calculated for every loop.
>
Perl being interpreted, you can apply known optimizations to it,
namely, (i+1)*(i+1) = i*i + 2*i + 1...


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

Date: Fri, 23 Mar 2007 16:49:50 +0000
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: How can I make my prime number generator better?
Message-Id: <4604052e$0$8755$ed2619ec@ptn-nntp-reader02.plus.net>

Abigail wrote:
> nikolas.britton@gmail.com (nikolas.britton@gmail.com) wrote on MMMMCMLII
> September MCMXCIII in <URL:news:1174610848.316977.94170@y80g2000hsf.googlegroups.com>:
> @@  How can I improve my code?... faster, better style, proper programming
> @@  techniques, better algorithm? Thanks!
> 
> A better algorith beats everything else.

Amen.

Learning about big O analysis beats learning about
language tricks.

  BugBear


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

Date: 23 Mar 2007 18:19:09 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: How can I make my prime number generator better?
Message-Id: <56ik0tF29efllU1@mid.dfncis.de>

Abigail  <abigail@abigail.be> wrote in comp.lang.perl.misc:
> nikolas.britton@gmail.com (nikolas.britton@gmail.com) wrote on MMMMCMLII
> September MCMXCIII in
> <URL:news:1174610848.316977.94170@y80g2000hsf.googlegroups.com>:
> @@  How can I improve my code?... faster, better style, proper programming
> @@  techniques, better algorithm? Thanks!
> 
> A better algorith beats everything else.
> 
> If you want to generate prime numbers starting from 1, sieves are your
> best option. They have been your best option since antiquity.
> 
> Here's an example of a sieve:
> 
> 
> #!/opt/perl/bin/perl -l
> 
> # A sieve based on base 6. Only possible primes are those numbers p
> # for which p mod 6 == 1, or p mod 6 == 5.
> #
> # Let n = 6 * k + l (0 <= l < 6). Then the bit b associated with n is
> # b = 2 * k + [undef, 0, undef, undef, undef, 1] -> [l]
> # In reverse, given a bit b, the corresponding number n is
> # n = 6 * int (b / 2) + [1, 5] -> [b % 2].

[snip

Ah, I remember that.  You developed it from a modest beginning (base 10,
if memory serves) to this form in steps, posting and discussing intermediate
implementations on clpm.  That was fun.

Anno


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

Date: Fri, 23 Mar 2007 10:20:01 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: how to use perl to redirect web page?
Message-Id: <4603fe31$0$492$815e3792@news.qwest.net>

Paul wrote:
> hi, there,
> 
> I am using perl to access specified web pages. this is my code. when I
> run the code, I get the following error: 302 Found. I checked and knew
> this is because of redirect.
> 
> Would you guys tell me how to solve this problem?

> use LWP::UserAgent;
> use HTTP::Headers;
> use HTTP::Cookies;

use strict;

> 
> 
> $lurl='http://www.abcddefg.com';
> $lbrowser = LWP::UserAgent->new;
> $lbrowser->timeout(5);
> $lcookie_jar = HTTP::Cookies->new;
> $lreq = new HTTP::Request('GET',$lurl);
> $lresponse = $lbrowser->simple_request($lreq);
[...]

Check the documentation for LWP::UserAgent.

perldoc LWP::UserAgent

You could search for "redirect", to find anything relevant.

e.g.

"The difference from request() is that simple_request() will not try
  to handle redirects or authentication responses. "

Always, always, always, start with the documentation.




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

Date: Fri, 23 Mar 2007 16:24:38 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: I dotn understand this error
Message-Id: <slrnf07s9m.svr.hjp-usenet2@yoyo.hjp.at>

On 2007-03-23 11:02, Michele Dondi <bik.mido@tiscalinet.it> wrote:
> On 23 Mar 2007 01:39:02 -0700, "?????" <hackeras@gmail.com> wrote:
>
>>The whole index.pl uses UTF-8 encoding and some special chars yes.
>>Is there a way to convert it to pure ASCII?
>
> recode(1), I guess. Or perl's piconv(1).

If you don't mind losing all the non-ascii chars ...

	hp

-- 
   _  | Peter J. Holzer    | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR       | to write clearly is like blaming English for
| |   | hjp@hjp.at         | the circumlocutions of bureaucrats.
__/   | http://www.hjp.at/ |	-- Charlton Wilbur in clpm


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

Date: Fri, 23 Mar 2007 14:24:17 -0400
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Re: On Java's Interface (the meaning of interface in computer programing)
Message-Id: <rXUMh.12$ha4.10@newsfe12.lga>

Lew wrote:
> But if Xah were being trollish, why didn't they jump on my response and 
> call me names?

I'm not sure he's a proper troll. Unfortunately, he seems to be the kind 
of person who thinks that reading "Java for Dummies" makes one a 
self-sufficient expert on Java and philosopher of programming languages 
to boot, and who is very eager to bestow upon the world all his 
brilliant insights.

At least, that explanation is consistent with his historic behavior.

-- 
John W. Kennedy
"Only an idiot fights a war on two fronts.  Only the heir to the throne 
of the kingdom of idiots would fight a war on twelve fronts"
  -- J. Michael Straczynski.  "Babylon 5", "Ceremonies of Light and Dark"
* TagZilla 0.066 * http://tagzilla.mozdev.org


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

Date: Fri, 23 Mar 2007 16:32:34 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <slrnf07soi.svr.hjp-usenet2@yoyo.hjp.at>

On 2007-03-20 18:20, Paul Lalli <mritty@gmail.com> wrote:
> I just don't see the point of using a method that's only sometimes
> valid as opposed to using one that's always valid.

There is no method which is always valid.

	hp

-- 
   _  | Peter J. Holzer    | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR       | to write clearly is like blaming English for
| |   | hjp@hjp.at         | the circumlocutions of bureaucrats.
__/   | http://www.hjp.at/ |	-- Charlton Wilbur in clpm


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

Date: Fri, 23 Mar 2007 10:41:05 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Problem with indirect variables
Message-Id: <x7vegs2cj2.fsf@mail.sysarch.com>

>>>>> "MD" == Marshall Dudley <mdudley@king-cart.com> writes:

  MD> Uri Guttman wrote:

  MD> &require_supporting_libraries
  MD> (__FILE__,__LINE__,"library/cgi-lib.pl");


where did that sub come from? do you really need to know the line and
file where you loaded something? if an error happened, perl will tell
you that info.

  MD> &ReadParse(*form_data);

that is an ancient, buggy and crappy cgi library. use CGI.pm and it even
has a compatible function for you. 

  >> i don't know what you
  >> think it is nor where you learned it but it makes no sense in how you
  >> describe it. it is a simple string expression. so it can't ever refer to
  >> anything else in perl especially other 'variables' as it is not even a
  >> proper variable name.
  >> 
  MD> $form_data{'string'} is a standard hash variable name.  I am not sure
  MD> why you think it is not.

because of what i said. YOU WERE MISSING THE LEADING $ when you
(mis)used it in your loop. THAT was a string and not a variable. anyhow
you can't do symrefs like that even if you wanted to (which you don't.

  MD> I am using the cgi-lib.pl library routine to get them.

use CGI.pm. cgi-lib hasn't been touched in over a decade.

  MD> If I inline it, I end up with 5*18 or 90 lines of code in what should
  MD> be able to be done in 6 or 7.
  >> 
  >> if you used a module it would be 1 line.
  >> 
  >> use HTML::Entities
  >> 
  >> encode_entities $_ for values %form_data ;
  >> 
  MD> OK, I will check the Entities module.

and CGI.pm and learn some basic perl. your concept of a loop over a hash
was nowhere close to what you wanted. 

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 258
**************************************


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