[18808] in Perl-Users-Digest
Perl-Users Digest, Issue: 976 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 24 14:06:27 2001
Date: Thu, 24 May 2001 11:05:20 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <990727519-v10-i976@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 24 May 2001 Volume: 10 Number: 976
Today's topics:
Re: Calling function reference nobull@mail.com
Re: Calling function reference <uri@sysarch.com>
Converting VB to PERL (passing parameters) <roinestad@hot_no_spam_mail.com>
Re: date question <vmurphy@Cisco.Com>
Re: Help slim down Perl code <uri@sysarch.com>
Re: Help slim down Perl code nobull@mail.com
Re: Help slim down Perl code <uri@sysarch.com>
Re: help: String Match <ren@tivoli.com>
Re: How can I convert a date (MM/DD/YYYY) to UTC time? <jimbo@soundimages.co.uk>
Re: How to install perl modules as local user <todd@designsouth.net>
Re: How to install perl modules as local user (Eric Smith)
Re: Implementing Orthogonality <mjcarman@home.com>
Invitation -- TESTING COMPUTER SOFTWARE (TCS2001) Confe (Peter Zuckerman)
looking 4 NMAP style perl module <jdonovan@beth.k12.pa.us>
looping through text to get info (Gary)
Re: looping through text to get info <peb@bms.umist.ac.uk>
Re: looping through text to get info <godzilla@stomp.stomp.tokyo>
Re: Match Parsing Glitch <abe@ztreet.demon.nl>
Re: module for combinatorics? (partitions etc) <joe+usenet@sunstarsys.com>
Re: perl tattoo <lmoran@wtsg.com>
Re: perl tattoo (Steven M. O'Neill)
Re: perl tattoo <hartleh1@westat.com>
Re: Permuting days of the week (James Weisberg)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 May 2001 17:53:08 +0100
From: nobull@mail.com
Subject: Re: Calling function reference
Message-Id: <u9k836664r.fsf@wcl-l.bham.ac.uk>
Kris Verbeeck <kris.verbeeck@chello.be> writes:
> Subject: Calling function reference
As it happens the problem you are experiencing in not specific to
references to functions, it applies equally to references to other
things.
> I got a small problem. I'm just experimenting a bit with calling
> subroutines dynamically. I have a class (package) in which one of
> the object fields is the name of a function that needs to be called.
"the name of a whatever" is a _symbolic_ reference to a whatever.
If it is possible to do so the best solution is simply to switch to
using hard references:
my $func = 'doit'; # Sybolic ref
my $func = \&doit; # Hard ref
If you really want to stick with symbolic references then read on...
> When I try calling this function, then perl complains that it
> doesn't find the function &class::func.
That's because symbolic references come in two flavours,
package-qualified and non-package-qualified. A defrerencing a
non-package-qualified symbolic reference in Perl will look in the
_current_ package's symbol table.
> This function does not exist in the class, but it exist in the
> global namespace.
When you say "global namespace" I think you mean "package main".
> How can I have a method in the class call that global function?
When you say "global function" I think you mean "function in package
main".
> package Test;
>
> sub process {
> my $self = shift;
> $self->{FUNC}->()
> }
> FUNC is defined as "doit", the name of an existing global function.
> When I execute this piece of code, perl responds with:
>
> Undefined subroutine &Test::doit called at Test.pm line xx.
> Any suggestions?
If you really want to keep it as "doit" and not change it to \&doit or
"main::doit" then...
package Test;
use strict; # Always a good idea
use warnings; # Also a good idea
sub process {
my $self = shift;
package main; # Temorarily change current package
no strict 'refs'; # Temorarily permit symbolic refs
$self->{FUNC}->();
}
BTW: If main::doit() calls caller() it will be told it was called by
'main' and not 'Test'. If you consider this to be is an problem then...
sub process {
my $self = shift;
do {
package main; # Temorarily change current package
no strict 'refs'; # Temorarily permit symbolic refs
\&{$self->{FUNC}};
}->();
}
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 24 May 2001 17:44:34 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Calling function reference
Message-Id: <x7d78y4p6m.fsf@home.sysarch.com>
>>>>> "n" == nobull <nobull@mail.com> writes:
n> If it is possible to do so the best solution is simply to switch to
n> using hard references:
n> my $func = 'doit'; # Sybolic ref
n> my $func = \&doit; # Hard ref
so if that is the best solution, why did you even clear up his symref
problem?
n> If you really want to stick with symbolic references then read on...
the problems with synrefs are well documented. read MJD's varvar pages
for some good reasons why they are dangerous. with subs it is even more
dangerous. how can you vet what sub will be called if you are passing in
any string? in a cgi, that is almost certain death.
a dispatch table is very easy to create and use and is safe.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: Thu, 24 May 2001 11:17:40 -0600
From: Michael Roinestad <roinestad@hot_no_spam_mail.com>
Subject: Converting VB to PERL (passing parameters)
Message-Id: <3B0D4234.259BCB49@hot_no_spam_mail.com>
Greetings,
I am trying to use an ActiveX function that will fill out two
Variant array of doubles. The problem is I do not know how to pass
these variables in a manner that they can be filled out like they do in
VB.
The setup for VB is as follows:
----------------------------------------------------------------------------------
Dim minExt As Variant
Dim maxExt As Variant
' Return the bounding box for the line and return the minimum
' and maximum extents of the box in the minExt and maxExt variables.
lineObj.GetBoundingBox minExt, maxExt
' minExt would contain for example [ 0.0 0.0 0.0 ] and extMax would
contain [ 1.0 1.0 0.0 ].
----------------------------------------------------------------------------------
I have tried different combinations for hours can't seem to find a
situation that works. Here is some of the methods I have tried.
$Entity->GetBoundingBox($minPoint, $maxPoint);
- or -
my $minPoint = Variant(VT_R8 | VT_ARRAY | VT_BYREF, qw(0 0 0));
my $maxPoint = Variant(VT_R8 | VT_ARRAY | VT_BYREF, qw(0 0 0));
$Entity->GetBoundingBox($minPoint, $maxPoint);
- or -
$Entity->GetBoundingBox(\$minPoint, \$maxPoint);
- or -
$Entity->GetBoundingBox(@$minPoint, @$maxPoint);
Any help at all would be greatly appreciated,
Michael Roinestad
------------------------------
Date: 24 May 2001 09:13:57 -0400
From: Vinny Murphy <vmurphy@Cisco.Com>
Subject: Re: date question
Message-Id: <m3ofsi28kq.fsf@vpnrel.cisco.com>
"Calin Guga" <CalinG@cfgroup.ca> writes:
> Hello there,
>
> Does anybody know how to convert a date in Perl, for ex. 01/01/2001 and
> to tell what day of the week that was(like Monday...Sunday)?
> Thanks a lot for your help,
> Calin
>
use Date::Manip;
You can download it from CPAN.
--
Vinny
------------------------------
Date: Thu, 24 May 2001 16:01:42 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Help slim down Perl code
Message-Id: <x7k8364ty1.fsf@home.sysarch.com>
>>>>> "BG" == Benjamin Goldberg <goldbb2@earthlink.net> writes:
BG> Falc2199 wrote:
>>
>> I have here a lot of reduntant code. I need a way to slim it down. It
>> bascially does the same thing over and over, open and a file and read
>> the contents into an array....
BG> foreach (qw( l o b )) {
BG> open my $fh, "<", "${_}Choice.txt"
BG> or die "can't open ${_}Choice.txt: $!";
BG> @{ $_ . "Carray" } = <$fh>;
BG> }
BG> This [untested] code should have the exact same effects as your code:
BG> that of sticking the contents of lChoice.txt, oChoice.txt, and
BG> bChoice.txt into the arrays named @lCarray, @oCarray, and @bCarray.
DIE EVIL SPAWN OF SYMREF!
do not show anyone how to use symrefs. they are dangerous and the wrong
way to handle dynamic structures. use a hash of arrays.
that goes for both the OP and ben.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: 24 May 2001 18:01:09 +0100
From: nobull@mail.com
Subject: Re: Help slim down Perl code
Message-Id: <u9g0du65re.fsf@wcl-l.bham.ac.uk>
Uri Guttman <uri@sysarch.com> writes:
> DIE EVIL SPAWN OF SYMREF!
>
> do not show anyone how to use symrefs.
Oh dear, I just did exactly that in another thread ("Calling function
reference"), am I now going to feel the rath of Uri decend upon me?
:-)
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 24 May 2001 17:41:25 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Help slim down Perl code
Message-Id: <x7g0du4pbz.fsf@home.sysarch.com>
>>>>> "n" == nobull <nobull@mail.com> writes:
n> Uri Guttman <uri@sysarch.com> writes:
>> DIE EVIL SPAWN OF SYMREF!
>>
>> do not show anyone how to use symrefs.
n> Oh dear, I just did exactly that in another thread ("Calling function
n> reference"), am I now going to feel the rath of Uri decend upon me?
n> :-)
it is spelled wrath, and yes, consider yourself slapped hard. :)
symrefs and eval should only be used when there is absolutely no other
way to do it. a few areas that qualify are generating perl code on the
fly (not closures, but perl text), and installing things in the symbol
table (as done with Exporter and in some AUTOLOAD uses). there are a few
other places as well and those areas are generally for experts so the
rule is no symrefs or eval unless you know why you must use them.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: 24 May 2001 10:45:37 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: help: String Match
Message-Id: <m31ypeda3i.fsf@dhcp9-172.support.tivoli.com>
On Wed, 23 May 2001, u518615722@spawnkill.ip-mobilphone.net wrote:
> we need to change a lot of email address
> in our files, the target line is like this
> string=user\@mailserver1.com
>
> I issued
> perl -pi.bak -e "s/user\\\@mailserver1.com/
> user\@mailserver2/g" `find ----'
>
> and
>
> perl -pi.bak -e "s/user\\@mailserver1.com/
> user\@mailserver2/g" `find ----'
>
> and
> perl -pi.bak -e "s/user\@mailserver1.com/
> user\@mailserver2/g" `find ----'
>
>
> None of them seems to work the trick.
>
> What is wrong with my perl command?
It may be that your shell is parsing the arguments and removing one
set of back-slashes. If you're using UNIX, change those double-quotes
(") to single-quotes ('). If you're on Windows, you may need to add
extra back-slashes.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Thu, 24 May 2001 13:52:44 +0100
From: "jimbo" <jimbo@soundimages.co.uk>
Subject: Re: How can I convert a date (MM/DD/YYYY) to UTC time?
Message-Id: <xH7P6.189$hD.5523@NewsReader>
"Bill Nelson" <william.c.nelson@gte.net> wrote in message
news:3B0AD81B.A2E890DF@gte.net...
> No way to do it with standard perl functions? I would rather not try
to
> bring in another module at this time.
Why?
Do you prefer to spend your time coding your project or reinventing the
wheel?
jimbo
;-)
------------------------------
Date: Thu, 24 May 2001 14:18:40 GMT
From: "Todd Smith" <todd@designsouth.net>
Subject: Re: How to install perl modules as local user
Message-Id: <4L8P6.64740$I5.13993424@news1.rdc1.tn.home.com>
Just put the directory in your home directory, where your scripts can find
it in @INC. It should work.
/home/you/Date/Manip.pm
"Eric Smith" <eric@fruitcom.com> wrote in message
news:slrn9gpm4d.3sm.eric@apple.fruitcom.com...
> like I can install a C application with
> ./configure --prefix=/home/foo
>
> --
> Eric Smith
------------------------------
Date: 24 May 2001 15:41:03 GMT
From: eric@fruitcom.com (Eric Smith)
Subject: Re: How to install perl modules as local user
Message-Id: <slrn9gqasb.3sm.eric@apple.fruitcom.com>
Todd Smith posted
> Just put the directory in your home directory, where your scripts can find
> it in @INC. It should work.
>
> /home/you/Date/Manip.pm
Yes, but that does not include the documentation, is prone to error and is
complicated when there are compiled files as well.
Only thing I dug up from RTFM:
How do I keep my own module/library
directory?
When you build modules, use the PREFIX option when generating
Makefiles:
perl Makefile.PL PREFIX=/u/mydir/perl
But this did not work as it only put that prefix on some of the install
dirs and not all so on 'make install' I got a permission denied.
This should be a real single step simple thing to do.
------------------------------
Date: Thu, 24 May 2001 09:11:36 -0500
From: Michael Carman <mjcarman@home.com>
Subject: Re: Implementing Orthogonality
Message-Id: <3B0D1698.923D8BE1@home.com>
Lou Moran wrote:
>
> I was recently handed The Proagmatic Programmer and I read it [...]
> I realized around page 50 that if this book was "right" I was doing
> nearly everything wrong regarding programming.
I haven't read that book (nor anything else about programming theory --
I'm an engineer not a computer scientist) but keep in mind that there
are different philosophies about programming. There is no one "right"
way.
> I agree totally with the orthogonality concepts but I don't know how
> I would use them using Perl.
Perhaps I'm misunderstanding, but I think of orthogonality as a feature
of the language, not as a modus operandi. At any rate, Perl isn't an
orthogonal language. It's sometimes referred to as a "diagonal" one.
This is a very deliberate feature of its design, and it's one reason
that there is a "natural" way to do almost anything in Perl.
> I am fairly lazy and C-x C-v my own code often
Not necessarily a bad thing, but don't cut & paste when you can either
a) proceduralize or b) modularize.
-mjc
------------------------------
Date: Thu, 24 May 2001 09:47:51 -0500
From: pazpax@clark.net (Peter Zuckerman)
Subject: Invitation -- TESTING COMPUTER SOFTWARE (TCS2001) Conference
Message-Id: <pazpax-2405010947510001@sdn-ar-009dcwashp251.dialsprint.net>
18th International Conference on TESTING COMPUTER SOFTWARE (TCS2001)
<http://www.uspdi.org/conference>
June 18-22, 2001; Washington, DC, USA
In cooperation with:
Association for Computing Machinery (ACM) SIGSoft
American Society for Quality (ASQ) Software Division
IEEE Reliability Society
Software Technology Support Center (STSC)
Conference management: U.S. Professional Development Institute
o Registration: For TCS2001 web registration go to:
<http://www.uspdi.org/conference/register.html>
o Complete TCS2001 program. The complete program is at:
<http://www.uspdi.org/conference/program.html>
o Workshops (2 days) and Tutorials (1 day). Experts provide comprehensive
technical sessions for software testing practitioners. For detailed
descriptions see:
<http://www.uspdi.org/conference/workshops.html>
Workshops:
1. "MITs Test Management Method" by Marnie Hutcheson (Internet
Development Associates)
2. "Fundamentals of Testing Software-Intensive Systems" by Selim
Aissi (Intel Corporation)
Tutorials:
3. "Overview of Testing" by Simon Mills (Ingenuity Unlimited)
4. "Test Planning Workshop" by Ross Collard (Independent
Consultant)
5. "The Automated Testing Lifecycle Methodology" by Elfriede Dustin
(BNA Software)
6. "Test Case Administration: Using XML to Exchange Test Case
Information Between the Business and Test Domains" by Hans
Hartmann (Generali Group)
7. "Evolutionary Project Management: How to Get Early Delivery and
Project" by Tom Gilb (Result Planning Ltd.)
8. "More Reliable Software Faster and Cheaper" by John Musa
(Consultant)
9. "Software Verification and Validation: An Overview for
Practitioners" by Steven R. Rakitin (Software Quality
Consulting)
10. "Guided Inspections" by Melissa L. Russ (Korson-McGregor)
o Keynote Sessions. Testing experts cover major topics of general interest:
"Real Time Priority and Risk Management for World Class Software
Quality" by Tom Gilb (Result Planning Ltd.)
"Is That Your Final Answer? Surviving Cross Examination in the
Courtroom" by Warren S. Reid (WSR Consulting Group)
"Efficient Testing with a Measure to Select Testing Strategies"
by John Musa (Consultant)
"Educators Join the Fray -- Tester Training for Undergraduates"
by Edward L. Jones (Florida A&M University)
"A World-Wide Web of Intrigue -- Deceptive Testing" by Simon Mills
(Ingenuity Unlimited)
"Wrap-up Session: Conference Reflections" by Marnie Hutcheson
(Internet Development Associates)
o Parallel Presentation Tracks: Thirty-two concurrent sessions will be
divided into four parallel track sessions, presenting an extensive variety
of management, automation, technology, technical and research topics and
issues.
o Vendor exhibit of products and services will provide valuable support
services and software information to your computer software testing
efforts.
o Networking:
Birds-of-a-Feather Sessions allow you to meet and network with the
speakers and other conference attendees, and discuss areas of
mutual interest.
Lunch Discussion Groups will facilitate the exploration of common
interest topics among the participants.
o For information:
Regular Mail: USPDI, 612 Ethan Allen Avenue, Suite 100;
Takoma Park, D 20912-5400 (USA)
Phone: (+1) 301-270-1033
Fax: (+1) 301-270-1040
E-mail: admin@uspdi.org (Subject: TCS2001)
o Detailed printed brochure can be requested:
from USPDI (see above),
or the Web at <http://www.uspdi.org/conference/brochure.html>
*****
------------------------------
Date: Thu, 24 May 2001 11:35:59 -0400
From: jeff <jdonovan@beth.k12.pa.us>
Subject: looking 4 NMAP style perl module
Message-Id: <jdonovan-41B938.11355824052001@iad-read.news.verio.net>
greetings
Im looking for a module or script that will execute a simple tcp port
scan like namp does.
this is the scan from nmap that I need written in perl.
./nmap -v -sS -O -p 3453 my.friggin.host
if anyone can point me to a MAN page or a URL that could explain to me
How to do this, or if anyone knows of a module already written that will
do the same feature. Please reply off list.
Thanks In Advance.
--jeff
------------------------------
Date: 24 May 2001 06:30:06 -0700
From: grobitaille@mail.com (Gary)
Subject: looping through text to get info
Message-Id: <a2051d6.0105240530.6c550e78@posting.google.com>
I have a text file that looks like this:
Database Name : database1
Type Code: 100030; Hard fault
Page format errors on OAM or text pages.
page id: 621
page header: 0x0000026D00000000000000000000002D000000020020FDFF000005254030
Header for 621, next 0, previous 0, id = 45:255
time stamp = 0x000000000002, next row = 32, level = 253
free offset = 1317, minlen = 0, status = 16432(0x4030)
Type Code: 100030; Hard fault
Page format errors on OAM or text pages.
page id: 1552
page header: 0x0000061000000000000000000000002D000000010020FDFF000000224030
Header for 1552, next 0, previous 0, id = 45:255
time stamp = 0x000000000001, next row = 32, level = 253
free offset = 34, minlen = 0, status = 16432(0x4030)
Database Name : database2
Type Code: 100030; Hard fault
Page format errors on OAM or text pages.
page id: 621
page header: 0x0000026D00000000000000000000002D000000020020FDFF000005254030
Header for 621, next 0, previous 0, id = 45:255
time stamp = 0x000000000002, next row = 32, level = 253
free offset = 1317, minlen = 0, status = 16432(0x4030)
Type Code: 100030; Hard fault
Page format errors on OAM or text pages.
page id: 1552
page header: 0x0000061000000000000000000000002D000000010020FDFF000000224030
Header for 1552, next 0, previous 0, id = 45:255
time stamp = 0x000000000001, next row = 32, level = 253
free offset = 34, minlen = 0, status = 16432(0x4030)
As you can see the are 2 sections 1 for database1 and the other is
database2. There are only 2 Hard faults for each however there could
be none or many many more. What I would like to do is get an output
that says "you had 'n' Hard faults on database: 'dbname'"
I assume this will most easily be done with a loop or nested loop but
am unsure how to proceed. Any help would be appreciated.
------------------------------
Date: Thu, 24 May 2001 16:56:19 +0100
From: Paul Boardman <peb@bms.umist.ac.uk>
Subject: Re: looping through text to get info
Message-Id: <3B0D2F23.F2F19DC6@bms.umist.ac.uk>
Gary wrote:
>
> I have a text file that looks like this:
>
> Database Name : database1
> Type Code: 100030; Hard fault
> Page format errors on OAM or text pages.
> page id: 621
> page header: 0x0000026D00000000000000000000002D000000020020FDFF000005254030
> Header for 621, next 0, previous 0, id = 45:255
> time stamp = 0x000000000002, next row = 32, level = 253
> free offset = 1317, minlen = 0, status = 16432(0x4030)
<snip rest of data>
> As you can see the are 2 sections 1 for database1 and the other is
> database2. There are only 2 Hard faults for each however there could
> be none or many many more. What I would like to do is get an output
> that says "you had 'n' Hard faults on database: 'dbname'"
assuming that the format of your file isn't going to change you could do
something like
perl -ne '($dbname) = $_ =~ m/^Database Name : (.*)$/ if /Database
Name/;$h{$dbname}++ if /Type Code:/;END{for(keys %h){print "you had
$errors{$_} Hard faults on database: $_\n"}}' text_file
that should all be one line by the way.
or the drawn out version would be
#!/usr/bin/perl -w
use strict;
my ($dbname, %errors);
open(DATA, "text_file") or die "couldn't open the data file : $!";
while(<DATA>){
($dbname) = $_ =~ m/^Database Name : (.*)$/ if /Database Name/;
$errors{$dbname}++ if /Type Code:/;
}
for(keys %errors){
print "you had $errors{$_} Hard faults on database: $_\n";
}
HTH
Paul
------------------------------
Date: Thu, 24 May 2001 09:07:51 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: looping through text to get info
Message-Id: <3B0D31D7.FF3961B7@stomp.stomp.tokyo>
Gary wrote:
> I have a text file that looks like this:
> Database Name : database1
(snipped)
I am curious to know what software produces
this database.
Godzilla!
------------------------------
Date: Thu, 24 May 2001 18:14:54 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: Match Parsing Glitch
Message-Id: <099qgt8atai9u7lg9f026jipdptd0uu6hl@4ax.com>
On Thu, 24 May 2001 00:51:41 -0700, Eric <nospam@xx.com> wrote:
> I'm trying to further tweak this unpack approach for a more detailed
> derivation to parse across some multi-lines of an expanded record
> offshute of the simple one-liner, but am in a glitch again.
>
> If this will, in fact, work...I'm having difficulty seeing how to write
> the ascii .csv file so that all 5 pieces of data will end up in a single
> line in the output file, since it appears I'm going to have to write the
> output before I loop to the next line in the in-file data record???
>
> Here's what I've done thus far, but would like to know if I am barking
> up the wrong tree by attempting to combine unpack functionality across
> multiple lines *and* be able to write the .csv file in one whack???
> Code follows (below)
...
Please, in future, use Copy/Paste if you want to show code here. People
can't see the difference between typo's and other mistakes.
Also make sure your program has warnings enabled and use()s strict.
Start your programs with:
#!/usr/bin/perl -w
use strict;
> # OBJECTIVES TO PARSE, CAPTURE & WRITE TO ASCII .csv FILE
> #
> # (Example above ... lines involved indicated by #(& Line #) at
> beginning)
> # 1. S Code (1 number) ... in 1st line ... the "5" in 1/5
> # 2. Address (25 characters) ... in 1st line ... follows 1/5 plus 2
> spaces
> # 3. SP ($x,xxx,xxx) ... at end of 1st line as "SP:"$x,xxx,xxx)
> # 4. SA (17 characters ... on 2nd line following "SA:"
> # 5. LA (17 characters) ... on 12th line following "LA:"
Are 4. and 5. _always_ true?
...
> while ($line = <INFILE>) #primary loop
> {
> if ($line =~ m//#\d{6}/) || ($line =~ m/^SO:.*) || ($line =~ m/^LA:.*)
That line has 4 errors in it and there is no opening curly for the if.
Why don't you use a "next unless" construct? There seems to be nothing
outside this if construct (within the loop).
> if ($line =~ m//#\d{6}/)
> {
> my ( $status, $addr, $price ) =
> unpack 'x13 A1 x2 A25 x28 A10', $line; #acquire/parse stats,
> address,price
> $price =~ tr/$,//d; #remove $ and comma on price
> $addr =~ s/^\s+|\s+$//g; #whitespace cleanup on address
> #and skip all other lines
> }
At this point ( $status, $addr, $price ) do not "exist" anymore as you
have declared them within the scope of the if.
> if $(line =~ m/^SO.*)
> {
> my ( $sa ) =
> unpack 'x18 A17', $line; #acquire/parse "LA:" (name)
> $sa =~ s/^\s+|\s+$//g; #whitespace cleanup on name
> }
Ditto for $sa
> if ($line =~ m/^LA.*/)
> {
> my ( $la ) =
> unpack 'x3 A17', $line; #acauire/parse "SA:" (name)
> $la =~ s/^\s+|\s+$//g; #whitespace cleanup on name
> }
Ditto for $la
> &writedatafile ($status, $addr, $price, $sa, $la)
> unless ($line !~ m//#\d{6}/) || ($line =~ m/^SO:.*) || ($line =~
> m/^LA:.*)
> #only call subroutine if valid
> #lines being parsed
That is an unnecessary unless, since you are still within the if
construct that ensures that condition.
> }
> }
> close (INFILE); #close input file
>
> ### SUBROUTINE TO PARSED DATA TO ASCII .CSV FILE
> sub writedatafile
> {
> my ($status, $addr, $price, $sa, $la ) = @_;
> open (FILE ,">>$datafile"); #open output file for append
> print FILE "$R,$status,\"$addr\",$price,$sa,$la\n"; #write data to file
> $R++; #increment record counter
> close (FILE); #close output file
> }
> # END OF PERL SCRIPT
Why do you want the output file to be reopened and closed for every
write?
Assuming each record (you want to process) has at least 12 lines, you
could try this for your main loop (untested!):
while ( my $line = <INPUT> ) {
next unless $line =~ /^#\d{6}/;
my( $status, $addr, $price ) =
unpack 'x13 A1 x2 A25 x28 A10', $line;
$price =~ tr/$,//d;
$addr =~ s/^\s+//g; #trailing spaces removed by unpack 'A'
$line = <INPUT>; #This line should start with 'SO'
die "Line 2 of record didn't start with 'SO'\n"
unless $line =~ /^SO/;
my( $sa ) = unpack 'x18 A17', $line;
$sa =~ s/^\s+//g;
$line = <INPUT> for 3..12; # skip some lines
die "Line 12 of record didn't start with 'LA'\n"
unless $line =~ /^LA/;
my( $la ) = unpack 'x3 A17', $line;
$la =~ s/^\s+//g;
writedatafile( $status, $addr, $price, $sa, $la );
}
--
Good luck, Abe
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -e '$_=sub{split//,pop;print pop while@_};&$_("rekcah lreP rehtona tsuJ")'
------------------------------
Date: 24 May 2001 13:58:52 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: module for combinatorics? (partitions etc)
Message-Id: <m3n182sk6b.fsf@mumonkan.sunstarsys.com>
Joe Schaefer <joe+usenet@sunstarsys.com> writes:
> Here's a (quadratic?) replacement for your earlier D sub:
[...code snipped...]
Here's a better quadratic version of mjd's "3am code". In an
earlier (canceled) post, I made some comment about the Q's
being binary coefficients, which was wrong, so I slept on it
to avoid making another careless remark. Apologies once again
for the error - I'll shut up now :-)
I've included some comments with pictures for reducing the
partitions, and included another implementation of Euler's
method for comparison/checking. It's feasible to generate
random partitions for N's as large as 200 or so. Beyond that
is probably pushing it.
% cat try.pl
#!/usr/bin/perl -l
# warnings disabled: lots of recursion here
use strict;
use POSIX 'ceil';
use vars qw/%Q @P/; # memo variables
my ($N, $P, $T) = @ARGV;
$N ||= 11;
$P ||= 4;
$T ||= 10000;
print "check: " . P($N) ." = " . Q(2*$N,$N);
my %h;
for (1..$T) {
++$h{join(",", random_partition($N, $P))};
print STDERR "$_" if $_ % 1000 == 0;
}
for my $k (sort {$h{$a}<=>$h{$b}} keys %h) {
print "$k\t$h{$k}";
}
# generate (equiprobably) a partition of $n into exactly $p parts
sub random_partition {
my ($n, $p) = @_;
my $k = 1; # minimum part size
my @partition = (); # the result
for my $d (reverse 0..($p-1)) {
my $total_leaves = 0;
my $next_part;
for my $i ($k..ceil($n/($d+1))) {
# $i is our 'guess' for the size of the next part
# $leaves = number of partitions of $n-$i into $d parts,
# each part has size >= $i
#
# e.g. n = 20 d xxxX
# i = 4 d xxxXX ~ Q(7,3)
# d = 3 d xxxXXXX
# iiifree
#
my $leaves = Q( $n-$i - $d*($i-1), $d );
$total_leaves += $leaves;
next unless $total_leaves;
$next_part = $i if rand() < $leaves/$total_leaves;
}
push @partition, $next_part;
$n -= $next_part;
$k = $next_part;
}
return @partition;
}
# Q = number partitions if N into exactly P parts
sub Q {
my ($n, $p) = @_; # number, partitions
return $n == $p if $n <= $p;
return $p == 1 if $p <= 1;
return $Q{$n,$p} if exists $Q{$n,$p};
# recursion formula
# 2 classes: partitions w/ and w/o a singleton
#
# n = 10 1 x p xX
# p = 3 p XXXX or p xXX
# p XXXXX p xXXXX
# Q(9,2) + Q(7,3)
return $Q{$n,$p} = Q($n-1,$p-1) + Q($n-$p,$p);
}
# P = Euler's recursion for total partions of $n (very fast)
sub P {
my $n = shift;
return $n >= 0 if $n <= 1;
return $P[$n] if defined $P[$n];
my ($m,$sum);
# loop condition: [x] (3[x] - 1) < 2 * $n
for (1 .. (( sqrt(1+24*$n)+1 ) / 6) ) {
$m = P($n - (($_*(3*$_ - 1))>>1) ) +
P($n - (($_*(3*$_ + 1))>>1) ) ;
if ( $_ % 2 == 1 ) { $sum += $m }
else { $sum -= $m }
}
return $P[$n] = $sum;
}
__END__
% time ./try.pl 11 4 1100
check: 56 = 56
1000
2,2,3,4 89
1,2,2,6 92
2,2,2,5 93
1,1,4,5 94
1,2,3,5 96
1,1,1,8 97
1,1,3,6 102
1,2,4,4 103
2,3,3,3 106
1,3,3,4 111
1,1,2,7 117
real 0m1.993s
user 0m1.950s
sys 0m0.010s
% time ./try.pl 100 10 10
check: 190569292 = 190569292
1,1,2,5,6,9,9,10,12,45 1
2,2,3,5,7,11,11,12,22,25 1
2,2,2,4,4,10,14,17,18,27 1
1,2,4,8,8,8,8,11,17,33 1
1,3,4,7,9,11,12,12,14,27 1
1,1,1,2,3,4,6,10,28,44 1
2,3,6,7,7,7,10,12,23,23 1
1,1,1,4,5,10,10,17,18,33 1
1,1,4,5,5,7,7,11,14,45 1
1,1,1,2,2,8,12,15,21,37 1
real 0m1.219s
user 0m1.160s
sys 0m0.050s
% time ./try.pl 200 5 10
check: 3972999029388 = 3972999029388
11,29,36,42,82 1
10,10,30,66,84 1
11,13,25,61,90 1
3,13,18,71,95 1
8,19,23,67,83 1
3,15,17,25,140 1
3,34,42,51,70 1
2,14,25,65,94 1
4,9,11,46,130 1
1,5,41,68,85 1
real 0m4.429s
user 0m3.910s
sys 0m0.090s
I'm sure this can be improved upon as well.
Best.
--
Joe Schaefer "A little inaccuracy sometimes saves a ton of explanation."
-- H. H. Munro (Saki)
------------------------------
Date: Thu, 24 May 2001 11:49:53 -0400
From: Lou Moran <lmoran@wtsg.com>
Subject: Re: perl tattoo
Message-Id: <ecbqgts6etgqubak99hb1oflspf9jov5o3@4ax.com>
On Wed, 23 May 2001 17:34:52 GMT, "John Hall" <jhall@ifxonline.com>
wrote wonderful things about sparkplugs:
>If I got the 7 line perl DECSS code tattoo'ed on my chest, if I went to the
>beach, would I be arrested by the feds?
>
Not if you applied ROT13 first!
--
BSOD? In my day we didn't have 0000FF!
lmoran@wtsg.com
------------------------------
Date: 24 May 2001 15:59:21 GMT
From: steveo@panix.com (Steven M. O'Neill)
Subject: Re: perl tattoo
Message-Id: <9ejb4p$sdb$3@news.panix.com>
Todd Smith <todd@designsouth.net> wrote:
>
>"John Hall" <jhall@ifxonline.com> wrote in message
>news:0xSO6.27782$vf6.2760109@news1.rdc1.sdca.home.com...
>> If I got the 7 line perl DECSS code tattoo'ed on my chest, if I went to
>the
>> beach, would I be arrested by the feds?
>>
>
>What if you do, then later there's a shorter way to do it using Perl6?
Will 6 be available in jail?
--
Steven O'Neill steveo@panix.com
www.cars-suck.org
------------------------------
Date: Thu, 24 May 2001 11:59:05 -0400
From: Henry Hartley <hartleh1@westat.com>
Subject: Re: perl tattoo
Message-Id: <3B0D2FC9.A4A18D39@westat.com>
Lou Moran wrote:
>
> On Wed, 23 May 2001 17:34:52 GMT, "John Hall" <jhall@ifxonline.com>
> wrote wonderful things about sparkplugs:
>
> >If I got the 7 line perl DECSS code tattoo'ed on my chest, if I went to the
> >beach, would I be arrested by the feds?
> >
> Not if you applied ROT13 first!
Or you could "use bleach;" first.
--
Henry Hartley
------------------------------
Date: Wed, 23 May 2001 15:43:25 GMT
From: chadbour@wwa.com (James Weisberg)
Subject: Re: Permuting days of the week
Message-Id: <xUQO6.6192$DW1.283167@iad-read.news.verio.net>
Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>>>>>> "James" == James Weisberg <chadbour@wwa.com> writes:
>James> Given the days of the week: Sun, Mon, Tue, Wed, Thu, Fri, Sat
>James>Iwould like to take various permuations and sequences of those strings
>James> and fill the days in between. Examples:
>
>James> Sun-Sat = "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
>James> Mon-Sun = "Mon,Tue,Wed,Thu,Fri,Sat,Sun"
>James> Wed-Tue = "Wed,Thu,Fri,Sat,Sun,Mon,Tue"
>James> etc, and sub-sequences:
>James> Sun-Tue = "Sun,Mon,Tue"
>James> Fri-Sun = "Fri,Sat,Sun"
>James> etc.
>James> So a function like fill($from, $to) would fill up to seven days
>James> $from - $to and $from != $to so Mon-Mon, for example, is invalid.
[...]
>If it's not a homework problem, you'll need to provide a little more
>context.
Not a homework problem, but without context I phrased it as a simple
challenge to the group. Obviously there's a straightfoward way to program
this, I just wanted to see what tricks you hackers might employ.
With context, there's not much to it. I have command-line arguments
to a program which pulls records out of databases on disk and I want to
be able to include or exclude certain ranges of days/dates. I've handled
the dates, now I considering days of the week. I want to support a simple
range syntax like Fri-Sun, for instance. I could ask the same question
for months of the year as well (e.g. Mar-Nov).
--
World's Greatest Living Poster
------------------------------
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 976
**************************************