[31354] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2606 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 21 14:09:47 2009

Date: Mon, 21 Sep 2009 11:09:12 -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           Mon, 21 Sep 2009     Volume: 11 Number: 2606

Today's topics:
    Re: Am I doing this wrong? Why does this seem so clumsy <danb_83@yahoo.com>
    Re: appending to a file. <rodbass63@gmail.com>
        Catch regular expression errors with eval <bmdavll@gmail.com>
    Re: Catch regular expression errors with eval <uri@StemSystems.com>
    Re: Catch regular expression errors with eval <source@netcom.com>
        efficiency of copying a file <bernie@fantasyfarm.com>
    Re: efficiency of copying a file <uri@StemSystems.com>
        elegant way to do the union of 2 strings <avilella@gmail.com>
    Re: elegant way to do the union of 2 strings <dannywoodz@yahoo.co.uk>
    Re: elegant way to do the union of 2 strings <jurgenex@hotmail.com>
    Re: elegant way to do the union of 2 strings <cartercc@gmail.com>
        FAQ 6.21 Are Perl regexes DFAs or NFAs?  Are they POSIX <brian@theperlreview.com>
        FAQ 7.6 What's an extension? <brian@theperlreview.com>
        FAQ 8.14 How do I modify the shadow password file on a  <brian@theperlreview.com>
        FAQ 8.49 How do I add a directory to my include path (@ <brian@theperlreview.com>
    Re: Newbie: Perl script to Windows and Linux executable <january.weiner@gmail.com>
    Re: Newbie: Perl script to Windows and Linux executable <mswanberg@gmail.com>
        printf vs. big numbers jidanni@jidanni.org
    Re: printf vs. big numbers <uri@StemSystems.com>
        random bits efficiently jidanni@jidanni.org
    Re: random bits efficiently <dannywoodz@yahoo.co.uk>
        Speeding up Solution to http://www.spoj.pl/problems/STR <himanshu.garg@gmail.com>
    Re: Speeding up Solution to http://www.spoj.pl/problems sln@netherlands.com
        Win32 and modules <pgodfrin@gmail.com>
    Re: Win32 and modules <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 20 Sep 2009 12:13:54 -0700 (PDT)
From: AggieDan04 <danb_83@yahoo.com>
Subject: Re: Am I doing this wrong? Why does this seem so clumsy (time,  datetime vs. DateTime)
Message-Id: <12d00bf4-1950-4b63-b48d-40eab265ce84@j19g2000vbp.googlegroups.com>

On Sep 19, 9:22=A0pm, Schif Schaf <schifsc...@gmail.com> wrote:
> The other day I needed to convert a date like "August 2009" into a
> "seconds-since-epoch" value (this would be for the first day of that
> month, at the first second of that day).
>
> In Python, I came up with this:
>
> ~~~~
> #!/usr/bin/env python
>
> import datetime
> import time
>
> time_in_sse =3D time.mktime(
> =A0 =A0 datetime.datetime(2009, 8, 1).timetuple()
> )

(datetime.datetime(2009, 8, 1) - datetime.datetime(1970, 1, 1)).days *
86400

But still, this should be part of the datetime class.


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

Date: Mon, 21 Sep 2009 10:39:34 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Re: appending to a file.
Message-Id: <343dfdf6-240c-4330-ac86-810ebef3e775@d34g2000vbm.googlegroups.com>

On Sep 19, 9:08=A0am, s...@netherlands.com wrote:
> On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <rodbas...@gmail.com> wrot=
e:
> >On Aug 31, 4:13=A0pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >> >>>>> "Nene" =3D=3D Nene =A0<rodbas...@gmail.com> writes:
>
> >Umm, I thought it worked but it's not working, probably because I'm
> >missing something. I created a new file and it has:
>
> >Listen80
> >Listen81
> >Listen82
>
> >But when I run:
>
> >#!/usr/bin/perl -w
> >use strict;
>
> >my $line_to_insert =3D "Listen8151\n";
> > =A0 =A0{
> > =A0 =A0 =A0local @ARGV =3D "/home/control/misc_perl_scripts/httpd.conf"=
;
> > =A0 =A0 =A0local $^I =3D ".bak"; # appended to the backup copy
> > =A0 =A0 =A0while (<>) {
> > =A0 =A0 =A0 =A0if ((/^Listen/..!/^Listen/) =3D~ /e/) { # if we're at th=
e end of
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0^
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0E
>
> >the Listens
> > =A0 =A0 =A0 =A0 =A0$_ =3D $line_to_insert . $_; # prepend the line to t=
he next
> >line
> > =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0print; # but print whatever we have
> > =A0 =A0 =A0}
> > =A0 =A0}
>
> >__END__
>
> >It doesn't print the $line_to_insert, and it doesn't print anything,
> >the file remains the same? Any clue?
>
> >Nene
>
> -sln- Hide quoted text -
>
> - Show quoted text -

I used a captial E install of a small case e, it still doesn't work.
if ((/^Listen/..!/^Listen/) =3D~ /E/) { # if we're at the end of

Did I undertand the last post correctly?

Nene






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

Date: Mon, 21 Sep 2009 07:59:31 -0700 (PDT)
From: David Liang <bmdavll@gmail.com>
Subject: Catch regular expression errors with eval
Message-Id: <22284a47-f5b8-4ae5-bd47-28cacf879492@f20g2000prn.googlegroups.com>

Sorry for the newcomer question; I couldn't find anything related.
What I'd like to do is compile and check if a regular expression is
proper:

    $re = '[bad';
    eval { $foo = qr/$re/; };
    if ($@) {
        # ...
    }

I was thinking the eval would catch normally fatal errors, but it
doesn't catch the regex error. The program crashes with:

    Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE bad/
at ...

Am I doing anything wrong? Is there a better way to do this?
Thanks.


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

Date: Mon, 21 Sep 2009 11:22:26 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Catch regular expression errors with eval
Message-Id: <87my4omifh.fsf@quad.sysarch.com>

>>>>> "DL" == David Liang <bmdavll@gmail.com> writes:

  DL> Sorry for the newcomer question; I couldn't find anything related.
  DL> What I'd like to do is compile and check if a regular expression is
  DL> proper:

  DL>     $re = '[bad';
  DL>     eval { $foo = qr/$re/; };
  DL>     if ($@) {
  DL>         # ...
  DL>     }

  DL> I was thinking the eval would catch normally fatal errors, but it
  DL> doesn't catch the regex error. The program crashes with:

  DL>     Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE bad/
  DL> at ...

use eval string instead. eval block catches dies but that is a syntax
error and is fatal. eval string catches syntax errors and you can do the
same check. i don't recommend eval string often but this is one of those
cases where it is useful.

uri


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

Date: Mon, 21 Sep 2009 08:55:29 -0700
From: David Harmon <source@netcom.com>
Subject: Re: Catch regular expression errors with eval
Message-Id: <Bu2dnbD5ktS8PyrXnZ2dnUVZ_sWdnZ2d@earthlink.com>

On Mon, 21 Sep 2009 07:59:31 -0700 (PDT) in comp.lang.perl.misc, David
Liang <bmdavll@gmail.com> wrote,
>Sorry for the newcomer question; I couldn't find anything related.
>What I'd like to do is compile and check if a regular expression is
>proper:
>
>    $re = '[bad';
>    eval { $foo = qr/$re/; };
>    if ($@) {

Seems to work for me...

$re = '[bad';
eval { $foo = qr/$re/; };
print join '#', split ' ', $@;

Yields:
Unmatched#[#in#regex;#marked#by#<--#HERE#in#m/[#<--#HERE#bad/#at#C:\USR\perl\t1.pl#line#5.


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

Date: Mon, 21 Sep 2009 10:28:05 -0400
From: Bernie Cosell <bernie@fantasyfarm.com>
Subject: efficiency of copying a file
Message-Id: <a73fb5l0q45n982k0rbj6ujjecq4rm08jj@216.168.3.66>

I was wondering if Perl [5.8] optimizes
   print THERE <HERE>
I have a 'HERE' that's about 2 gigs and I was about to write the above when
it occurred to me that creating a 2-gig list and then iterating through it
to print it out was likely not going to be the best way to copy the
file..:o)  My guess is it'll be better to a read/write loop with a suitably
large LENGTH.

   /Bernie\
-- 
Bernie Cosell                     Fantasy Farm Fibers
bernie@fantasyfarm.com            Pearisburg, VA
    -->  Too many people, too few sheep  <--          


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

Date: Mon, 21 Sep 2009 11:20:23 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: efficiency of copying a file
Message-Id: <87r5u0miiw.fsf@quad.sysarch.com>

>>>>> "BC" == Bernie Cosell <bernie@fantasyfarm.com> writes:

  BC> I was wondering if Perl [5.8] optimizes
  BC>    print THERE <HERE>

  BC> I have a 'HERE' that's about 2 gigs and I was about to write the
  BC> above when it occurred to me that creating a 2-gig list and then
  BC> iterating through it to print it out was likely not going to be
  BC> the best way to copy the file..:o) My guess is it'll be better to
  BC> a read/write loop with a suitably large LENGTH.

that would be a killer waste of cpu and ram. this is one of the times i
would recommend shelling out to cp as it will be much faster. or
possibly using the file::copy module which would be much more
efficient. not only is your method going to slurp in the whole file, if
you didn't enable whole file mode, it will do it as a list of lines
which means all that parsing for newlines which kills the cpu. dealing
with gb size files as a whole is not something perl does well.

uri


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

Date: Mon, 21 Sep 2009 06:12:00 -0700 (PDT)
From: avilella <avilella@gmail.com>
Subject: elegant way to do the union of 2 strings
Message-Id: <ec22db5a-df00-48ae-90d9-148595e054d4@l35g2000vba.googlegroups.com>

Hi,

I am looking for an elegant way to do the union of two string. For
example:

$seq1 = "--- --- GAA --- GGA";
$seq2 = "AAC TGG --- --- ---";

The rule is to do the union of the letters, leaving the dash symbol
when both have a dash in a given position:

$union = "AAC TGG GAA --- GGA";

Anyone? Maybe using a regular expression?

Thanks,

Albert.


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

Date: Mon, 21 Sep 2009 14:31:04 +0100
From: Danny Woods <dannywoodz@yahoo.co.uk>
Subject: Re: elegant way to do the union of 2 strings
Message-Id: <50pr9kl90n.fsf@yahoo.co.uk>

avilella <avilella@gmail.com> writes:

> Hi,
>
> I am looking for an elegant way to do the union of two string. For
> example:
>
> $seq1 = "--- --- GAA --- GGA";
> $seq2 = "AAC TGG --- --- ---";
>
> The rule is to do the union of the letters, leaving the dash symbol
> when both have a dash in a given position:
>
> $union = "AAC TGG GAA --- GGA";

Perhaps:

$seq1 =~ s/-/substr($seq2, pos($seq1), 1)/eg;

Works for your test case.  Perhaps you have more data to test it
against?

Cheers,
Danny.


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

Date: Mon, 21 Sep 2009 08:54:47 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: elegant way to do the union of 2 strings
Message-Id: <1t7fb5l069gpatadhk7gbnqq2hojiru3p6@4ax.com>

avilella <avilella@gmail.com> wrote:
>I am looking for an elegant way to do the union of two string. For
>example:
>
>$seq1 = "--- --- GAA --- GGA";
>$seq2 = "AAC TGG --- --- ---";
>
>The rule is to do the union of the letters, leaving the dash symbol
>when both have a dash in a given position:
>
>$union = "AAC TGG GAA --- GGA";
>
>Anyone? Maybe using a regular expression?

Sometimes just using different terminology solves the mysterie:

In sequence 1 (logically those are not strings but as you indicated
yourself they are sequences) you want to replace each '---' element with
the corresponding element from sequence 2.

#untested, sketch only
my @a = split(/ /, $seq1);
my @b = split(/ /,$seq2);
for my $i (0..$#a) {
	if ($a[i] eq '---') {$a[i] = $b[i]};
}
my $res = join (' ', @a);

This can probably be optimized somewhat by using map() and computing the
result elements in the argument of the join() but this way the logic is
very accessible. 

jue


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

Date: Mon, 21 Sep 2009 09:31:39 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: elegant way to do the union of 2 strings
Message-Id: <5b221c1d-d30b-4372-b67d-151f6518dbe6@a6g2000vbp.googlegroups.com>

On Sep 21, 9:12=A0am, avilella <avile...@gmail.com> wrote:
> Hi,
>
> I am looking for an elegant way to do the union of two string. For
> example:
>
> $seq1 =3D "--- --- GAA --- GGA";
> $seq2 =3D "AAC TGG --- --- ---";
>
> The rule is to do the union of the letters, leaving the dash symbol
> when both have a dash in a given position:
>
> $union =3D "AAC TGG GAA --- GGA";

I would approach this from the standpoint of comparing arrays rather
than looking at strings. Split each sequence and then iterate through
the arrays.

use strict;
use warnings;
my $s1 =3D "--- --- GAA --- GGA";
my $s2 =3D "AAC TGG --- --- ---";
my @s1 =3D split / /, $s1;
my @s2 =3D split / /, $s2;
my @s3 =3D ();
my $len =3D @s1; #get the length
for (my $i =3D 0; $i < $len; $i++)
{
  $s3[$i] =3D '---' if $s1[$i] eq '---' and $s2[$i] eq '---';
  $s3[$i] =3D $s1[$i] if $s1[$i] =3D~ /[A-Z]{3}/ and $s2[$i] eq '---';
  $s3[$i] =3D $s2[$i] if $s1[$i] eq '---' and $s2[$i] =3D~ /[A-Z]{3}/;
  $s3[$i] =3D '***'  if $s1[$i] =3D~ /[A-Z]{3}/ and $s2[$i] =3D~ /[A-Z]{3}/=
;
}
my $s3 =3D join ' ', @s3;
print $s3;
exit(0);


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

Date: Sun, 20 Sep 2009 22:00:05 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 6.21 Are Perl regexes DFAs or NFAs?  Are they POSIX compliant?
Message-Id: <FBxtm.216152$Qg6.170438@newsfe14.iad>

This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

6.21: Are Perl regexes DFAs or NFAs?  Are they POSIX compliant?

    While it's true that Perl's regular expressions resemble the DFAs
    (deterministic finite automata) of the egrep(1) program, they are in
    fact implemented as NFAs (non-deterministic finite automata) to allow
    backtracking and backreferencing. And they aren't POSIX-style either,
    because those guarantee worst-case behavior for all cases. (It seems
    that some people prefer guarantees of consistency, even when what's
    guaranteed is slowness.) See the book "Mastering Regular Expressions"
    (from O'Reilly) by Jeffrey Friedl for all the details you could ever
    hope to know on these matters (a full citation appears in perlfaq2).



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Mon, 21 Sep 2009 04:00:02 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 7.6 What's an extension?
Message-Id: <6TCtm.385$Aw4.76@newsfe04.iad>

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

7.6: What's an extension?

    An extension is a way of calling compiled C code from Perl. Reading
    perlxstut is a good place to learn more about extensions.



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Mon, 21 Sep 2009 16:00:07 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 8.14 How do I modify the shadow password file on a Unix system?
Message-Id: <bqNtm.1468$jq2.275@newsfe03.iad>

This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

8.14: How do I modify the shadow password file on a Unix system?

    If perl was installed correctly and your shadow library was written
    properly, the getpw*() functions described in perlfunc should in theory
    provide (read-only) access to entries in the shadow password file. To
    change the file, make a new shadow password file (the format varies from
    system to system--see passwd for specifics) and use pwd_mkdb(8) to
    install it (see pwd_mkdb for more details).



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Mon, 21 Sep 2009 10:00:03 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 8.49 How do I add a directory to my include path (@INC) at runtime?
Message-Id: <D8Itm.438609$Ta5.231212@newsfe15.iad>

This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

8.49: How do I add a directory to my include path (@INC) at runtime?

    Here are the suggested ways of modifying your include path, including
    environment variables, run-time switches, and in-code statements:

    the PERLLIB environment variable
                $ export PERLLIB=/path/to/my/dir
                $ perl program.pl

    the PERL5LIB environment variable
                $ export PERL5LIB=/path/to/my/dir
                $ perl program.pl

    the perl -Idir command line flag
                $ perl -I/path/to/my/dir program.pl

    the use lib pragma:
                use lib "$ENV{HOME}/myown_perllib";

    The last is particularly useful because it knows about machine dependent
    architectures. The lib.pm pragmatic module was first included with the
    5.002 release of Perl.



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Mon, 21 Sep 2009 11:04:35 +0200 (CEST)
From: January Weiner <january.weiner@gmail.com>
Subject: Re: Newbie: Perl script to Windows and Linux executable versions.
Message-Id: <h97fj3$iog$1@sagnix.uni-muenster.de>

On 2009-09-18, Harry <simonsharry@gmail.com> wrote:
> Hey folks,
>
> I have a perl script that I'd like to be able to run on Windows and
> Linux. Is this easily possible?

Here is how I normally do it.

1) I have a Linux and a Windows installation, both with a full Perl 
   installation. I make sure that my script works on both installations. 
   Note that the Windows installation is usually just a virtualbox with 
   Windows on it, sharing the perl script folder with the host Linux 
   machine.

2) The only additional Perl module I use is the perl packer (PAR::Packer) 
   which comes with the "pp" utility (pp.exe on Windows). I use this to 
   construct the executables. I run it on the Linux machine and on the 
   Windows machine separately.

      pp -o hello hello.pl

      or

      pp.exe -o hello.exe hello.pl

3) test this on a Windows machine (again, a virtualbox) without perl
   installation. Presto.

This works really well except for the long startup times for even small
scripts. However, it does work and I have not found a better solution.

j.



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

Date: Mon, 21 Sep 2009 09:54:23 -0700 (PDT)
From: MSwanberg <mswanberg@gmail.com>
Subject: Re: Newbie: Perl script to Windows and Linux executable versions.
Message-Id: <76e34fd2-3fc6-48a1-9a5e-3872b08fd23a@z28g2000vbl.googlegroups.com>

On Sep 18, 11:11=A0am, Harry <simonsha...@gmail.com> wrote:
> On Sep 18, 8:59=A0pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
> wrote:
>
> > If you want some sort of single executable that runs, without any
> > runtime support, on a variety of operating systems (even if they're all
> > the same x86 architecture) - I can't think of any programming language
> > where this is possible.
>
> What I want is
> =A0 =A0 an EXE for Windows, and
> =A0 =A0 an ELF for Linux
> each of which runs without any dependence on the Perl installation.
>
> Thanks, RGB (for taking the time to write)!

I think you were on the right track with PAR.  Check out Hack #40 in
the book "Perl Hacks".  I've not done it myself but it will walk you
through making self-extracting PARs that can be executed.

-Mike


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

Date: Mon, 21 Sep 2009 09:31:31 +0800
From: jidanni@jidanni.org
Subject: printf vs. big numbers
Message-Id: <873a6h6q30.fsf@jidanni.org>

What does one need to do to make this not blow up when $k reaches 10?
$ perl -wle 'for $k(10){for(qw/b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
%b	11111111111111111111111111111111
%d	-1
%lld	-1
%ld	-1
%Ld	-1
%o	37777777777
%u	4294967295
%x	ffffffff


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

Date: Mon, 21 Sep 2009 00:11:35 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: printf vs. big numbers
Message-Id: <874oqxrl6w.fsf@quad.sysarch.com>

>>>>> "j" == jidanni  <jidanni@jidanni.org> writes:

  j> What does one need to do to make this not blow up when $k reaches 10?
  j> $ perl -wle 'for $k(10){for(qw/b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
  j> %b	11111111111111111111111111111111
  j> %d	-1
  j> %lld	-1
  j> %ld	-1
  j> %Ld	-1
  j> %o	37777777777
  j> %u	4294967295
  j> %x	ffffffff

 perl -wle 'for $k(10){for(qw/f g b d lld ld Ld o u x/){printf "%%$_\t%$_\n",10**$k}}'
%f	10000000000.000000
%g	1e+10
%b	1001010100000010111110010000000000
%d	10000000000
%lld	10000000000
%ld	10000000000
%Ld	10000000000
%o	112402762000
%u	10000000000
%x	2540be400

get a 64 bit machine!

perl overflows integers to floats and you are seeing that. notice i
added f and g formats. try those. printing an integer format of a too
large float is going to generate nonsense since there aren't enough bits
to convert the number correctly.

uri


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

Date: Mon, 21 Sep 2009 23:23:23 +0800
From: jidanni@jidanni.org
Subject: random bits efficiently
Message-Id: <87r5u09tvh.NURD.fsf@jidanni.org>

Gentlemen, no big deal, but in
$ w3m -dump http://en.wikipedia.org/wiki/Talk:The_Hum |grep -A1 perl
$ perl -we 'for(0..33){print int rand 2?"_":"-"}'
_------_-___-_--__-__----____-_-__

how could I make such strings more efficiently, perhaps via
pack(), unpack(), vec(), sprintf "%b", tr/01/_-/, etc.?

I.e., I want to print a line of random _ and -'s. However, being a nerd,
I want to know the most efficient, even though I'm only doing it once in
a lifetime, for that Wikipedia comment, which you are welcome to add to too.


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

Date: Mon, 21 Sep 2009 17:27:42 +0100
From: Danny Woods <dannywoodz@yahoo.co.uk>
Subject: Re: random bits efficiently
Message-Id: <50fxagclfl.fsf@yahoo.co.uk>

jidanni@jidanni.org writes:

> Gentlemen, no big deal, but in
> $ w3m -dump http://en.wikipedia.org/wiki/Talk:The_Hum |grep -A1 perl
> $ perl -we 'for(0..33){print int rand 2?"_":"-"}'
> _------_-___-_--__-__----____-_-__
>
> how could I make such strings more efficiently, perhaps via
> pack(), unpack(), vec(), sprintf "%b", tr/01/_-/, etc.?

I'm suspicious of the repeated calls to rand().  Nothing quite beats
avoiding function calls if at all possible, so my (half-hearted, wide
open to criticism/corrections) attempt looks like this:

for ( $value = rand(2 ** 32), $mask = 1 << (32 - 1) ; $mask ; $mask >>= 1 )
{
  print (($value & $mask) ? "-" : "_");
}

Cheers,
Danny.



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

Date: Mon, 21 Sep 2009 08:36:27 -0700 (PDT)
From: "++imanshu" <himanshu.garg@gmail.com>
Subject: Speeding up Solution to http://www.spoj.pl/problems/STRLCP/
Message-Id: <5fd2ed75-2581-4578-b702-6eeaa695a7f7@u16g2000pru.googlegroups.com>

Hi,

      I've written this script to solve the algorithmic problem in
subject but it always times out. Assuming that the script is otherwise
correct, could you suggest possible improvements please :-

my $T = <STDIN>;
chomp($T);

while ($T--)
{
	my $s = <STDIN>;
	chomp $s;
	$s = [split(//, $s)];

	my $op_count = <STDIN>;
	chomp $op_count;

	while ($op_count--)
	{
		my $op;
		my ($operation, $operand1, $operand2) = split (/\s/, $op);

		$operand1--;
		if ($operation eq 'Q')
		{
			$operand2--;
			if ($operand1 == $operand2)
			{
				print $#{$s} - $operand1 + 1, "\n";
			}
			else
			{
				my $orig_operand1 = $operand1;
				while ( $s->[$operand1++] eq $s->[$operand2++] ) {}
				print $operand1-$orig_operand1-1;
				print  "\n";
			}
		}
		elsif ($operation eq 'I')
		{
			splice @{$s}, $operand1, 0, $operand2;
		}
		else
		{
			$s->[$operand1] = $operand2;
		}
	}
}

Thanks,
++imanshu


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

Date: Mon, 21 Sep 2009 10:51:36 -0700
From: sln@netherlands.com
Subject: Re: Speeding up Solution to http://www.spoj.pl/problems/STRLCP/
Message-Id: <upcfb5t8aleijg6f77t09fr6itf4j94gv4@4ax.com>

On Mon, 21 Sep 2009 08:36:27 -0700 (PDT), "++imanshu" <himanshu.garg@gmail.com> wrote:

>Hi,
>
>      I've written this script to solve the algorithmic problem in
>subject but it always times out.

What do you mean by "times out"?
Each <IN> is expecting a buffered '/n' terminated input.

> Assuming that the script is otherwise
>correct, could you suggest possible improvements please :-

But the script does no error checking for either eof on
input or validity/range checks of some variables.

If you intend to do this by actually keyboard entry, <> won't
return until an enter is pressed.

If you intend to pipe data in from a file using type or echo,
it might be better to pass in a filename and handle data with an
open. If its from a program that constructs dynamic data, it still
may be better to have it write to a file, but a pipe would work.

use strict;
use warnings;

>
>my $T = <STDIN>;
>chomp($T);
>
>while ($T--)
>{
>	my $s = <STDIN>;
what if $s is undefined?

>	chomp $s;
>	$s = [split(//, $s)];
>
>	my $op_count = <STDIN>;
what if $op_count is undefined?

>	chomp $op_count;
>
>	while ($op_count--)
>	{
>		my $op;
>		my ($operation, $operand1, $operand2) = split (/\s/, $op);
What happens when you insert print "$operation, $operand1, $operand2\n";
here?

>
>		$operand1--;
>		if ($operation eq 'Q')
>		{
>			$operand2--;
>			if ($operand1 == $operand2)
>			{
>				print $#{$s} - $operand1 + 1, "\n";
>			}
>			else
>			{
>				my $orig_operand1 = $operand1;
>				while ( $s->[$operand1++] eq $s->[$operand2++] ) {}
>				print $operand1-$orig_operand1-1;
>				print  "\n";
>			}
>		}
>		elsif ($operation eq 'I')
>		{
>			splice @{$s}, $operand1, 0, $operand2;
>		}
>		else
>		{
>			$s->[$operand1] = $operand2;
>		}
>	}
>}
>
>Thanks,
>++imanshu

Your code is in dire need of some comments, at least if
your asking for help.

It also needs some debugging prints here and there.
my $in = *DATA;
#my $in = *STDIN;

print ">enter 'T': ";
my $T = <$in>;
chomp($T);
print "$T\n";

while ($T--)
{
	print "\twhile loop counter = $T\n";

	print "\t>enter 's':";
	my $s = <$in>;
	chomp $s;
	print " $s\n";

	print "\t>enter 'op_count':";
	my $op_count = <$in>;
	chomp $op_count;
	print " $op_count\n";

	while ($op_count--)
	{
		print "\t\topcount = $op_count\n";
	}
}

__DATA__
3
12
6
3
8
2
5

-sln


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

Date: Sun, 20 Sep 2009 20:16:02 -0700 (PDT)
From: pgodfrin <pgodfrin@gmail.com>
Subject: Win32 and modules
Message-Id: <756dfc6a-451a-493c-b18d-8bc08534165b@o35g2000vbi.googlegroups.com>

Greetings,

Is there anything out there to make using Win32 modules (Activestate)
not require dusting off my C skills? Case in point - Win32::Service:

GetStatus(hostName, serviceName, status)
The third argument must be a hash reference that will be populated
with entries corresponding to the SERVICE_STATUS structure of the
Win32 API.

Microsoft says:

C++

typedef struct _SERVICE_STATUS {
  DWORD dwServiceType;
  DWORD dwCurrentState;
  DWORD dwControlsAccepted;
  DWORD dwWin32ExitCode;
  DWORD dwServiceSpecificExitCode;
  DWORD dwCheckPoint;
  DWORD dwWaitHint;
}SERVICE_STATUS, *LPSERVICE_STATUS;

Do I have remember how to do all that "or-ing" and "and-ing" again?
pg


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

Date: Mon, 21 Sep 2009 06:44:58 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Win32 and modules
Message-Id: <qfgjo6-kpf1.ln1@osiris.mauzo.dyndns.org>


Quoth pgodfrin <pgodfrin@gmail.com>:
> 
> Is there anything out there to make using Win32 modules (Activestate)
> not require dusting off my C skills? Case in point - Win32::Service:
> 
> GetStatus(hostName, serviceName, status)
> The third argument must be a hash reference that will be populated
> with entries corresponding to the SERVICE_STATUS structure of the
> Win32 API.
> 
> Microsoft says:
> 
> C++
> 
> typedef struct _SERVICE_STATUS {
>   DWORD dwServiceType;
>   DWORD dwCurrentState;
>   DWORD dwControlsAccepted;
>   DWORD dwWin32ExitCode;
>   DWORD dwServiceSpecificExitCode;
>   DWORD dwCheckPoint;
>   DWORD dwWaitHint;
> }SERVICE_STATUS, *LPSERVICE_STATUS;
> 
> Do I have remember how to do all that "or-ing" and "and-ing" again?

Yes. It's not difficult though: you don't need to know any C, just a
how & and | work.

Ben



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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 2606
***************************************


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