[25266] in Perl-Users-Digest
Perl-Users Digest, Issue: 7511 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 12 14:05:51 2004
Date: Sun, 12 Dec 2004 11:05:08 -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 Sun, 12 Dec 2004 Volume: 10 Number: 7511
Today's topics:
Consecutive Numbers <graham.drabble@lineone.net>
Re: Consecutive Numbers <mritty@gmail.com>
Re: Consecutive Numbers (Anno Siegel)
Re: distinguish between binary text and regular text (Anno Siegel)
Re: distinguish between binary text and regular text <jurgenex@hotmail.com>
Re: Fast and high-quality Web Site Creation (Anno Siegel)
Re: mod_perl headaches (Andrew Burton)
Re: mod_perl headaches (Andrew Burton)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 12 Dec 2004 17:52:25 GMT
From: Graham Drabble <graham.drabble@lineone.net>
Subject: Consecutive Numbers
Message-Id: <Xns95BDB5D279355grahamdrabblelineone@ID-77355.user.dfncis.de>
I have a file that contains a list of numbers. I'm trying to process
the file to find out how many rows start with 4 consecutive numbers
(either ascending or decending). Currently I've got
use strict;
use warnings;
open (IN, '4bell.txt') or die "Can't open IN: $!";
my $runs = 0;
while(<IN>){
chomp;
my $first = substr($_,0,1);
my $asc = $first . $first+1 . $first+2 . $first+3;
my $des = $first . $first-1 . $first-2 . $first-3;
if (/^($asc|$des)/){
$runs++
}
}
print "There were $runs runs\n";
IN
12345867
23457658
34568765
43215687
13245678
Prints
There were 4 runs
which is correct. However I can't help but think there must be a
shorter solution but can't think of it. Any ideas? The file could
contain up to 5000 lines.
--
Graham Drabble
If you're interested in what goes on in other groups or want to find
an interesting group to read then check news.groups.reviews for what
others have to say or contribute a review for others to read.
------------------------------
Date: Sun, 12 Dec 2004 13:46:21 -0500
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Consecutive Numbers
Message-Id: <cpi3ml$f6i$1@misc-cct.server.rpi.edu>
Graham Drabble wrote:
> I have a file that contains a list of numbers. I'm trying to process
> the file to find out how many rows start with 4 consecutive numbers
> (either ascending or decending). Currently I've got
>
> use strict;
> use warnings;
>
> open (IN, '4bell.txt') or die "Can't open IN: $!";
>
> my $runs = 0;
> while(<IN>){
> chomp;
> my $first = substr($_,0,1);
> my $asc = $first . $first+1 . $first+2 . $first+3;
> my $des = $first . $first-1 . $first-2 . $first-3;
> if (/^($asc|$des)/){
> $runs++
> }
> }
> print "There were $runs runs\n";
>
> IN
> 12345867
> 23457658
> 34568765
> 43215687
> 13245678
>
> Prints
> There were 4 runs
>
> which is correct. However I can't help but think there must be a
> shorter solution but can't think of it. Any ideas? The file could
> contain up to 5000 lines.
Here's my solution. I don't know if it's better or worse, but it's
certainly different (a couple extra test cases given - 6 'valid' lines
total):
#!/usr/bin/perl
use strict;
use warnings;
my $rows;
while (<DATA>){
chomp;
my @nums = (split //)[0..3];
$rows++ if (grep {$nums[$_] == $nums[$_-1]+1} 1..3) == 3;
$rows++ if (grep {$nums[$_] == $nums[$_-1]-1} 1..3) == 3;
}
print "Total of $rows rows\n";
__DATA__
12345867
23457658
34568765
43215687
13245678
67893131
12335668
13456789
76542109
------------------------------
Date: 12 Dec 2004 18:58:17 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Consecutive Numbers
Message-Id: <cpi4c9$6c2$1@mamenchi.zrz.TU-Berlin.DE>
Graham Drabble <graham.drabble@lineone.net> wrote in comp.lang.perl.misc:
> I have a file that contains a list of numbers. I'm trying to process
> the file to find out how many rows start with 4 consecutive numbers
> (either ascending or decending). Currently I've got
Please don't speak of numbers when you mean digits. Your example is hard
to understand without that distinction. That is a waste of time.
> use strict;
> use warnings;
>
> open (IN, '4bell.txt') or die "Can't open IN: $!";
>
> my $runs = 0;
> while(<IN>){
> chomp;
> my $first = substr($_,0,1);
> my $asc = $first . $first+1 . $first+2 . $first+3;
> my $des = $first . $first-1 . $first-2 . $first-3;
> if (/^($asc|$des)/){
> $runs++
> }
> }
> print "There were $runs runs\n";
>
> IN
> 12345867
> 23457658
> 34568765
> 43215687
> 13245678
>
> Prints
> There were 4 runs
>
> which is correct. However I can't help but think there must be a
> shorter solution but can't think of it. Any ideas? The file could
> contain up to 5000 lines.
Worry about speed when the program runs too slow, not before.
I don't know about shorter, or even faster, but your way of building
a regular expression for each case is certainly clumsy and limited.
Regular expressions are for text, but what you are dealing with are
numbers (despite my stressing that they are digits). Use arithmetic
operations for numbers, the other way lies, for instance, a famous
Y2K bug.
Your ascending or descending sequences of digits can be considered
arithmetic sequences with a difference of +1 or -1. That the elements
happen to be single-digit numbers is irrelevant here. So write a
general routine that recognizes arithmetic sequences (of any length):
sub is_arithmetic_sequence {
my $delta = shift;
while ( @_ > 1 ) {
$_ + $delta == $_[ 0] or return 0 for shift;
}
return 1;
}
That can deal with ascending and descending sequences, so you don't
have to code for both.
To extract the four-digit sequences from each line, a regex is
appropriate. So the main loop is
my $runs = 0;
while ( <DATA> ) {
my @seq = /(\d)(\d)(\d)(\d)/ or next;
$runs ++ if
is_arithmetic_sequence( -1, @seq) or
is_arithmetic_sequence( 1, @seq);
}
print "There were $runs runs\n";
That should deal with a few thousand lines with no big problems on
current hardware. The code gives the right result for your data,
but isn't severely tested.
Anno
------------------------------
Date: 12 Dec 2004 16:20:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: distinguish between binary text and regular text
Message-Id: <cphr3i$1dg$1@mamenchi.zrz.TU-Berlin.DE>
zvika <zglickman@il.bphx.com> wrote in comp.lang.perl.misc:
> How can I distinguish between binary text and regular text ?
That distinction doesn't make much sense. All data is binary on a
standard computer. Text is simply (binary) data intended to be
decoded and rendered in form of characters. There is no way you can
look at a piece of data and decide "This is text" or "This isn't".
> For example:
> I want to convert the value of SYSOT to hex values,
> and to keep the value of SYSOT1 the same.
>
> 01 SYSOT VALUE '^@^X^@^A^@^AA' PIC X(07).
> 01 SYSOT1 VALUE 'ABC' PIC X(07).
From your example it looks like you want to recognize control characters.
That can be done with a regex and a POSIX character class:
$string =~ /[[:cntrl:]]/ and print "Contains a control character\n";
If you are looking for something else, check out other predefined
character classes or build your own.
Anno
------------------------------
Date: Sun, 12 Dec 2004 16:20:06 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: distinguish between binary text and regular text
Message-Id: <Wc_ud.4784$Qp.4406@trnddc01>
zvika wrote:
> How can I distinguish between binary text and regular text ?
That depends on _your_ definition of 'binary' versus 'regular' in the
context of _your_ program, e.g. do you consider the byte sequence for a
Chinese character in UTF-8 which is encoded in Base64 to be regular text or
binary text?
> For example:
> I want to convert the value of SYSOT to hex values,
> and to keep the value of SYSOT1 the same.
>
> 01 SYSOT VALUE '^@^X^@^A^@^AA' PIC X(07).
> 01 SYSOT1 VALUE 'ABC' PIC X(07).
Sorry, this example doesn't help much to explain what you want to do.
One possible interpretation could be printable versus non-printable
characters. In that case you may want to check the 'print' class in 'perldoc
perlre'.
If you mean something else then please let us know what you mean.
jue
------------------------------
Date: 12 Dec 2004 16:22:28 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Fast and high-quality Web Site Creation
Message-Id: <cphr84$1dg$2@mamenchi.zrz.TU-Berlin.DE>
[Newsgroups trimmed]
CNA Programming Group <cna_pgroup@yahoo.com> wrote in comp.lang.perl.misc:
> I'm experienced PHP/Coldfusion developer from Russia with more than 5
> years of experience in the field.
Wrong group. Go look for one with "jobs" in the title.
Anno
------------------------------
Date: 12 Dec 2004 18:39:48 GMT
From: tuglyraisin@aol.commcast (Andrew Burton)
Subject: Re: mod_perl headaches
Message-Id: <20041212133948.14202.00001683@mb-m13.aol.com>
>Well probably the mod_perl mailing list would be the most appropriate.
Thank you. I will try and report there. Hack finding and searching the
archive may solve my problem. Thanks.
>What do you mean by "refuses to run"?
Basically, it just doesn't run. When I comment out the libperl.so line, Apache
runs. I can view files, see directories, PHP works, etc. However, when I
leave the libperl.so line uncommented, nothing happens. I start up Apache with
the standard "apachectl start" command, it says that it's started, but trying
to access webpages does nothing.
>Can you please show the Apache directives you use to load mod_perl?
I think so. Is this what you mean:
perl Makefile.PL USE_APXS=1 WITH_APXS=/usr/local/apache/bin/apxs EVERYTHING=1
...or did you mean the LoadModule line from httpd.conf:
#LoadModule perl_module libexec/libperl.so
(It's currently commented out, so ignore the pound at the beginning of the
line.)
>Clear the error_log, try to start Apache and tell us exactly what
>happens and what's in the log afterwards.
I stopped Apache, uncommented the line, and cleared error_log -- I went inn
with vi, and deleted every line; that seemed safer than deleting the file and
touch'ing a new one. When I re-ran Apache, Apache still didn't activate and
the error_log file was still empty.
Andrew Burton - tuglyraisin at aol dot com
Felecia Station on Harvestgain - Jarod Godel in Second Life
------------------------------
Date: 12 Dec 2004 18:43:42 GMT
From: tuglyraisin@aol.commcast (Andrew Burton)
Subject: Re: mod_perl headaches
Message-Id: <20041212134342.14202.00001684@mb-m13.aol.com>
>I've had problems with dynamic linkers getting confused by this in the
>past
I wonder if maybe I tried to load it both ways when I was make'ing mod_perl.
>A simpler work-around is to build mod_perl as a static extension, rather
>than as a DSO.
I'll look at making a static extension. The DSO just seemed to work nicely with
PHP5, so I thought it might make installing mod_perl -- something I have only
ever gotten to work once -- easier. Thanks.
Andrew Burton - tuglyraisin at aol dot com
Felecia Station on Harvestgain - Jarod Godel in Second Life
------------------------------
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 V10 Issue 7511
***************************************