[14017] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1427 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 19 12:15:52 1999

Date: Fri, 19 Nov 1999 09:15:35 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <943031734-v9-i1427@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 19 Nov 1999     Volume: 9 Number: 1427

Today's topics:
        perl optimizes away need for /o? was [/o in regexp with lee.lindley@bigfoot.com
    Re: perl optimizes away need for /o? was [/o in regexp  <uri@sysarch.com>
    Re: perl optimizes away need for /o? was [/o in regexp  lee.lindley@bigfoot.com
    Re: Perl programming sytle (Kragen Sitaker)
    Re: Perl programming sytle <rootbeer@redcat.com>
    Re: Perl programming sytle (Martien Verbruggen)
    Re: Perl programming sytle <cassell@mail.cor.epa.gov>
    Re: Perl programming sytle <uri@sysarch.com>
    Re: problem with DBM delete (Kragen Sitaker)
    Re: problem with DBM delete <cassell@mail.cor.epa.gov>
    Re: Q: DBI.pm (odd warnings) <slanning@bu.edu>
    Re: regular expression help <cassell@mail.cor.epa.gov>
    Re: Regular Expressions <cassell@mail.cor.epa.gov>
    Re: replace " " with &nbsp; (Peter J. Kernan)
    Re: Save As box... printing an image to the screen" <cassell@mail.cor.epa.gov>
    Re: Script Effiency (Tad McClellan)
    Re: Setting Environment variables from a perl script <mhc@Eng.Sun.COM>
    Re: Several Perl questions related to speed and memory  <aqumsieh@matrox.com>
    Re: Several Perl questions related to speed and memory  (Kragen Sitaker)
        Simple help <tomclark@btinternet.com>
    Re: Simple help <vincent.murphy@cybertrust.gte.com>
    Re: Simple help <tomclark@btinternet.com>
    Re: Simple help <lr@hpl.hp.com>
    Re: Simple help <lr@hpl.hp.com>
    Re: Simple help <lr@hpl.hp.com>
    Re: this pareser won't do 4 visual hebrew (Tad McClellan)
        Using variables with pattern matching nkaiser@my-deja.com
    Re: WINNT, Email, MS Outlook and MS Exchange dwiesing@my-deja.com
        Writing Modules (David Waters)
    Re: Writing Modules <moseley@best.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: 18 Nov 1999 23:18:17 GMT
From: lee.lindley@bigfoot.com
Subject: perl optimizes away need for /o? was [/o in regexp with mod_perl]
Message-Id: <8121fp$nmb$1@rguxd.viasystems.com>

Want to see something really scary?  It might shake your faith in the
documentation though, so don't look if you are a perldoc fanatic.

ksh: cat x
use re 'debug';
$a = 'this';
$b = 'that';
$x = 'another';
for ("this is another fine mess", 
	"that is some other guy", 
	"yet another") {
        print "true for $1\n" if /($a|$b|$x)/;
		#$x = 'other';
}

ksh: (perl x 2>&1) | grep compiling
compiling RE `(this|that|another)'
ksh:

It would appear that if the string in m// doesn't change, then the
regexp does not get recompiled.  If I uncomment the reassignment to
$x in the loop, then it does get recompiled, but only one more time,
not 2 more times.  So it is only recompiled if the string changes.

Is the /o modifier now a vestigal artifact?  I can think of
situations where you would still use it, but they are much fewer and
could now be handled through use of qr{}.

-- 
// Lee.Lindley   /// I used to think that being right was everything.
// @bigfoot.com  ///  Then I matured into the realization that getting
////////////////////   along was more important.  Except on usenet.


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

Date: 18 Nov 1999 18:58:30 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: perl optimizes away need for /o? was [/o in regexp with mod_perl]
Message-Id: <x73du3s66x.fsf@home.sysarch.com>

>>>>> "ll" == lee lindley <lee.lindley@bigfoot.com> writes:

  ll> Is the /o modifier now a vestigal artifact?  I can think of
  ll> situations where you would still use it, but they are much fewer and
  ll> could now be handled through use of qr{}.

only partially vestigial. it will still keep the regex from being
recompiled even if one of its variables has changed. but modern perl is
smart enought to optimize away recompiling a regex if none of its
variables has changed. this is actually better than /o as it does what
is really wanted in most cases.

as for all of /o's functionality being handled by qr// i will let ilya
argue that point. i don't worry about things that small in perl.

and if you think the docs are not accurate in this area, you know the
chorus:

patches welcome!

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: 19 Nov 1999 02:04:12 GMT
From: lee.lindley@bigfoot.com
Subject: Re: perl optimizes away need for /o? was [/o in regexp with mod_perl]
Message-Id: <812b6s$qec$1@rguxd.viasystems.com>

Uri Guttman <uri@sysarch.com> wrote:
[snip]
:>             .... i don't worry about things that small in perl.

I suspect lots of grinning smirks out there after they read
this line from you.  :-)

It is actually not a small thing in the problem set which began this
thread.  If you use re's from outside sources, then this is important
knowledge.  The documentation is quite adamant that you should
use m//o for efficiency.  The reality is demonstratably different.
Even the cookbook is out of date with respect to this issue.
This is not such a "small thing."

:>and if you think the docs are not accurate in this area, you know the
:>chorus:

:>patches welcome!

Actually, I kind of doubt that patches to perlre are welcome.
I suspect it is due a full rewrite.  Since I have spent much time
reading perlre over the past several months, I gave some thought
to this potential rewrite.  Then I shuddered a bit and went on
to something more pleasant.  When you look at the breadth of the
task -- the range of prior knowledge of the intended audience
and the complexity of telling the full story of the regexp engine --
well, "daunting task" becomes a term with a new perspective.

-- 
// Lee.Lindley   /// I used to think that being right was everything.
// @bigfoot.com  ///  Then I matured into the realization that getting
////////////////////   along was more important.  Except on usenet.


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

Date: Thu, 18 Nov 1999 19:42:09 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Perl programming sytle
Message-Id: <lEYY3.25907$YI2.1151516@typ11.nn.bcandid.com>

In article <38341E74.D0CD9922@gisma.de>, James Mohr  <j.mohr@gisma.de> wrote:
> I was recently "chastised" by someone for programming perl on  WinNT
> like I programm C on UNIX. He insisted that I should programm more
> "perl-like" and many of the constructs he suggested as changes were
> definately not C-like, and were definately harder to read. (IMO)

Difficulty of reading has a lot to do with your experience.

I find that some "perl-like" constructs, while they require that I
learn more of Perl, end up being better in the long run.  Typically
they eliminate extra variables and make the code shorter, and generally
are further toward the semantic end of the mechanical/semantic
spectrum.  (That is, they say what I want to do, not how I want to do
it.)

This results in code that contains fewer potential bugs and can be more
quickly understood by someone who knows Perl well.  It also means the
code is faster to write, largely because there's less you have to think
about.

On the other hand, if your target audience is C programmers who
occasionally speak Perl, by all means, write C-like Perl.  You wouldn't
want to write like Faulkner if you were trying to communicate with
second-year English-as-a-second-language students, or three-year-olds.

>I figure that computers have reached the point where there is no need
>for making "tighter" code.

In general, you're right -- and Perl is probably not the best choice
for the exceptions anyway.  Using Perl idioms is about making your code
more expressive, more readable, and easier to write, only incidentally
faster.

You might want to look at Larry Wall's thoughts on natural languages
and Perl: http://www.wall.org/~larry/natural.html.

-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: Thu, 18 Nov 1999 15:11:59 -0800
From: Tom Phoenix <rootbeer@redcat.com>
Subject: Re: Perl programming sytle
Message-Id: <Pine.GSO.4.10.9911181511060.16575-100000@user2.teleport.com>

On Thu, 18 Nov 1999, Brett W. McCoy wrote:

> Typing
> 
> 	foreach $i (1..10) {
> 
> is a lot easier to type than
> 
> 	for($i = 0; $i < 10; $i++) {

Yes, and the former counts from 1 to 10 a lot better than the latter. :-)

-- 
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



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

Date: Fri, 19 Nov 1999 00:05:56 GMT
From: mgjv@comdyn.com.au (Martien Verbruggen)
Subject: Re: Perl programming sytle
Message-Id: <Ev0Z3.130$eR.3932@nsw.nnrp.telstra.net>

On Thu, 18 Nov 1999 20:59:44 GMT,
	Brett W. McCoy <bmccoy@foiservices.com> wrote:

> 	foreach $i (1..10) {
               (0..9)

> 	for($i = 0; $i < 10; $i++) {

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Unix is user friendly. It's just
Commercial Dynamics Pty. Ltd.   | selective about its friends.
NSW, Australia                  | 


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

Date: Thu, 18 Nov 1999 16:16:44 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Perl programming sytle
Message-Id: <383496EC.9FBC6F40@mail.cor.epa.gov>

James Mohr wrote:
[snip]
> Now I know I am free to write my programs. However, I would like to hear
> other people's thoughts on perl programming style.

In my opinion, Perl programming style has some important 
issues you might want to consider.  I'll only touch on two,
though.

Perl compiles down to opcodes which are lovingly hand-krufted
in C.  If you write code which translates directly to a smaller
number of such opcodes, your code should run faster.  Since
Perl is not C, this should be a consideration.

Perl has a number of features which help prevent one-off
errors and other nasties.  Using 
    foreach $elt (@array) { ...
instead of the equivalent C-ish loop is a way to lower
the bugs-to-code ratio.  Similar Perl thingies can help
create maintainable code.. even though said code may
be less readable to a C programmer.  But is one also
then obligated to write code so that Lisp programmers
can read it easily?  And awk programmers?  And FORTRAN
programmers?  And Python programmers?  Writing code
specifically for another language's practitioners seems
like an odd requirement.  You probably do not have
people asking you to write your C code to look more like
COBOL, do you?  :-)

David
-- 
David Cassell, OAO                     cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician


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

Date: 18 Nov 1999 19:21:39 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Perl programming sytle
Message-Id: <x7u2mjqqjw.fsf@home.sysarch.com>


if i don't like what i am reading, it is bad perl style!

<FINE_PRINT>of course i am allowed to change my taste at any time
without prior notification to the coder</<FINE_PRINT>

:-)

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Thu, 18 Nov 1999 19:45:17 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: problem with DBM delete
Message-Id: <hHYY3.25913$YI2.1152186@typ11.nn.bcandid.com>

In article <38343E17.90732267@datassociates.com>,
Chris Stigas  <chris@datassociates.com> wrote:
>yes.  The compare works.. however I think that when the hash entry is
>deleted, it somehow, "messes up" the continuation of the loop.

That's right.  Don't insert into or delete from a hash while you're
iterating over it -- it doesn't work.  perldoc -f each will tell you
more.


-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: Thu, 18 Nov 1999 16:21:07 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: problem with DBM delete
Message-Id: <383497F3.70ED495@mail.cor.epa.gov>

[text re-arranged because Chris didn't post his reply after
Bill's message.  Naughty, naughty!]

Chris Stigas wrote:
> Bill Moseley wrote:
> 
> > Chris Stigas (chris@datassociates.com) seems to say...
> > > Hello,
> > >
> > > I am having a problem with this small piece of code:
> > >
> > >  while (($key, $value) = each(%MAILDB)) {
> > >         if($MAILDB{$key} eq 0){
> >                            ^^
> > You want to compare numbers?

> yes.  The compare works.. 

Maybe.  But if you want to compare numerically, please use
the == comparison instead.  'eq' is better when you want to
compare strings, like '0' or '3.141592653589'.

>                          however I think that when the hash entry is
> deleted, it somehow, "messes up" the continuation of the loop.  Maybe
> somehow throwing off the 'each(%MAILDB)' command.  Any ideas or
> thoughts?  I have only been programming with perl for a few days, so I'm
> not too experienced with Perl.

Probably.  Don't do inserts or deletes when you're each()ing
through the hash.  You can get out of order and hose the
iterator.

David
-- 
David Cassell, OAO                     cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician


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

Date: 18 Nov 1999 21:40:12 -0500
From: Scott Lanning <slanning@bu.edu>
Subject: Re: Q: DBI.pm (odd warnings)
Message-Id: <kusk8nf6w6r.fsf@bottom.bu.edu>

Jeff Zucker <jeff@vpservices.com> writes:

> Scott Lanning wrote:
> > erik_lembke@my-deja.com writes:
> > > Use of uninitialized value at (eval 21) line 18.
> > >
> > > (my line 18 is empty)
> > 
> > Or so you claim! Hmmph!

> And why shouldn't we believe that claim?  The error message refers
> to line 18 of the eval, not line 18 of the code.

Hmmph!
You're pretty easy to persuade.

-- 
"If lightning is the anger of the gods, the gods are concerned mostly
with trees." --Lao Tse


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

Date: Thu, 18 Nov 1999 16:34:12 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: regular expression help
Message-Id: <38349B04.A394E76F@mail.cor.epa.gov>

Alexis PONCET wrote:
> 
> Hi !

Howdy!

> I'm posting here the question because Perl programmer are better than PHP
> programmer in regular expression (well I think so ;-))

Flatterer.

Actually, this probably means that you're posting in the
wrong place, because the regex which a Perlite might produce
is likely to use optimizing features which do not exist in
PHP.  Isn't there a French newsgroup specifically on PHP?

And you didn't even tell anyone the structure of the string
you wanted to examine.

And, if you really need true parsing [just in case you
meant the technical version of 'parse' instead of the
natural-language usage], you can't get there with just
a regex.  You'd need to go with a real parser.. of which
Perl has several.

David
-- 
David Cassell, OAO                     cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician


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

Date: Thu, 18 Nov 1999 15:22:57 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Regular Expressions
Message-Id: <38348A51.CDABAF4E@mail.cor.epa.gov>

Darin Dugan wrote:
[snip]
> if (/\;.+\;/) {

Umm, why are you backslashing those semicolons?  You don't need
to.  It just makes the regex harder to grok.

>   print "found it\n";
> } # end if
[snip]

And if you would indent better [read the perlstyle pages],
you wouldn't need to label the '}' with comments.

David the nitpicker
-- 
David Cassell, OAO                     cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician


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

Date: 18 Nov 1999 19:41:56 GMT
From: pete@theory2.phys.cwru.edu (Peter J. Kernan)
Subject: Re: replace " " with &nbsp;
Message-Id: <slrn838lk4.7q4.pete@theory2.phys.cwru.edu>

On Wed, 10 Nov 1999 18:51:54 GMT, \
  Paul W. Hanbury, Jr. <pwhst@pitt.edu> wrote:
 .=$string =~ s/^( *)/'&nbsp;' x length $1/e;
             s/\G /&nbsp;/g;                 #golf
-- 
  Pete


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

Date: Thu, 18 Nov 1999 15:55:47 -0800
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Save As box... printing an image to the screen"
Message-Id: <38349203.665934BC@mail.cor.epa.gov>

Tom Phoenix wrote:
> 
> On 18 Nov 1999, Frosty wrote:
> 
> > Newsgroups: comp.lang.perl.misc, alt.comp.perlcgi.freelance
> 
> > Actually, for this NG, the question should be "who can I hire to fix this?"
> 
> No, but you're close. That question is for a newsgroup with 'jobs' in the
> name. There is no question for this newsgroup. Cheers!

I believe Frosty was referring to the *other* newsgroup in the
Newsgroups: line.

My newsfeed doesn't carry alt.comp.perlcgi.freelance .
But, oddly enough, I'm not bothered a bit.  :-)

David
-- 
David Cassell, OAO                     cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician


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

Date: Fri, 19 Nov 1999 06:44:53 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Script Effiency
Message-Id: <slrn83ae1l.1ug.tadmc@magna.metronet.com>

On Wed, 17 Nov 1999 17:47:14 -0800, Steven Lybeck <alanis@softhome.net> wrote:

>There are a few things that
>could be done more efficiently. Like when you are reading the file, you can
>take out the entire "while ($person=<ALUMNI>) {}" loop and do exactly the
>same thing with "@people = <ALUMNI>;"


   1) that is _less_ (memory) efficient, not more efficient.

   2) they do NOT do exactly the same thing.


-- 
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: 18 Nov 1999 16:26:29 -0800
From: Mike Coffin <mhc@Eng.Sun.COM>
Subject: Re: Setting Environment variables from a perl script
Message-Id: <8p64sejqqbu.fsf@Eng.Sun.COM>

ps123@my-deja.com writes:

> I would like to set a user-defined environment variable from a
> perl script, and export it to the parent shell (I am converting a shell
> script to a perl script). Can someone tell me as to how I can achieve
> it?

It's impossible.  A child process cannot modify the environment of its
parent.  (A shell cannot do this, either, so it should not be necessary
in converting a shell script to Perl.)

-mike


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

Date: Thu, 18 Nov 1999 13:14:18 -0500
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: Several Perl questions related to speed and memory use
Message-Id: <x3yln7vzmyu.fsf@tigre.matrox.com>


"HIS Newsgroups" <jacks@pobox.com> writes:

> [I had earlier posted this to comp.lang.perl but I see it's not read as much
> as this one; sorry for the multiple postings.]

That is because comp.lang.perl has been dead for a while now. You
shouldn't post there anymore.

> I am developing a Perl script (5.00503 on NT) that processes many lines of
> text. I am looking into ways to speed it up. Unfortunately, because of
> company regulations, I cannot post all of my script here.

A lot of people around here frown upon that kind of attitude.

> For one pattern match, I would like to find out if there is a faster way to
> do this. In the code fragment below, the $csCookie is composed of a number
> of keyword=value pairs, spearated by semicolons.The XYZZY is composed of
> upper- and lower-case letters and/or digits. Is there a faster way to do
> this pattern match? I could possibly use the \w character, and ignore the
> fact that it would allow the underscore character to be in the string also,
> if this was significantly faster than the pattern used below. Or, what if I
> used /XYZZY=(\w*/ and, if that succeeded, then look for the underscore
> - would that be faster than what I am using below?

I doubt it. I highly doubt that this is the bottle neck in your
program. You could probably do something like:

	/XYZZY=([^_\s]+)/

but again, I don't think it will make a big difference.

Since you are using a recent version of Perl, look into using the qr//
operator to compile your regular expression first.

> if ($csCookie =~ m/XYZZY=([A-Za-z0-9]+)/) {
>     $ValidStrings ++;
>     return $1;
> }
> 
> 
> When I run my script on 30 files (averaging around 125,000 KB each), the
> rate at which the script processes each file decreases as it progresses
> through the 30 files. The script uses anonymous arrays, hashs, etc. But, I
> am fairly certain I have gotten all the "memory leaks" out of it. So, I'm
> not sure if memory is the problem; what are some of the common reasons for a
> decrease in speed as Perl runs?

You're probably doing something wrong. Maybe you need to clear some
hash, or array before you start reading each file. It's very hard to
say since you don't show us any real code.

> If I wanted to check to make sure I was undef'ing all my references and
> deleting hash keys no longer used, is there a way to dump or look at all the
> memory my script has allocated and see the reference counts?

No, but I would limit the scope of any variable to the absolute
minimum, just to be sure.

HTH,
--Ala



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

Date: Thu, 18 Nov 1999 20:03:57 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Several Perl questions related to speed and memory use
Message-Id: <NYYY3.25931$YI2.1153989@typ11.nn.bcandid.com>

In article <x3yln7vzmyu.fsf@tigre.matrox.com>,
Ala Qumsieh  <aqumsieh@matrox.com> wrote:
>"HIS Newsgroups" <jacks@pobox.com> writes:
>I doubt it. I highly doubt that this is the bottle neck in your
>program. You could probably do something like:
>
>	/XYZZY=([^_\s]+)/
>
>but again, I don't think it will make a big difference.

Have you benchmarked?

>Since you are using a recent version of Perl, look into using the qr//
>operator to compile your regular expression first.

The expression he posted will be compiled at compile-time anyway; it
doesn't interpolate any variables.
-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: Thu, 18 Nov 1999 23:27:27 -0000
From: "Tom Clark" <tomclark@btinternet.com>
Subject: Simple help
Message-Id: <81221a$ak$1@uranium.btinternet.com>

Hi all,

Was wondering if some kind soul could help me.

Am trying to write a Perl script that will extract data from a text file and
present it in a new one.

The (beginning of the) original file from which the data has to extracted
looks like this:

A0: Astronomy - Advanced
  S6300 Professor Alethia Williams
    9901770 Cory Peterson                  56
    9911224 Frances Fowler                51
    9920645 Sarah Watt                      59
    9921786 Henry Peterson                72
    9925656 William Peterson              62

The lines with numbers that begin 99 are records of students taking the
course unit (its fairly self-explanatory).

I need to extract all the lines with students (ignoring the lines with the
teachers and the unit title), and put this into a text file that looks like
this:

    9901770 Cory Peterson
    9911224 Frances Fowler
    9920645 Sarah Watt
    9921786 Henry Peterson
    9925656 William Peterson

I've been trying to use a script that looks like this:

$filename = "Results.txt" ;
open (RHANDLE, $filename)
  or die "can't open $filename\n" ;
while ($line = <RHANDLE>) {
  if ($line =~ /99/) {
  chomp($line) ;
  $pos = index($line,"99") ;
  $id = substr($line, 4, ($pos+7)) ;
  $name = substr($line, ($pos+8)) ;
  $students{$id} = $name ; }}
$outfilename = "students.txt" ;
open (GFILE, ">$outfilename")
  or die "can't open $outfilename\n" ;
while (($id,$name) = each(%students)) {
  print GFILE "$id\t$name\n" ; }
print "Press return to quit\n" ;

Now all you experts will quickly see what's wrong here and tell me how to
fix it.... ;))

Its adapted from another program written by someone else.  I'm no Perl
genius - as I'm sure is apparent!.  I understand pretty much what's going
on - its just selecting the right bits of the lines that is eluding me.  The
file (ultimately) is going to put the students data into an 'insert into'
command in SQL.

Please be gentle...

Many thanks,

Tom
Stirling, Scotland




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

Date: Thu, 18 Nov 1999 23:44:46 GMT
From: Vincent Murphy <vincent.murphy@cybertrust.gte.com>
Subject: Re: Simple help
Message-Id: <xjgd7t7l5zl.fsf@gamora.ndhm.gtegsc.com>

>>>>> "Tom" == Tom Clark <tomclark@btinternet.com> writes:

    Tom> The (beginning of the) original file from which the data has to extracted
    Tom> looks like this:

    Tom> A0: Astronomy - Advanced
    Tom>   S6300 Professor Alethia Williams
    Tom>     9901770 Cory Peterson                  56
    Tom>     9911224 Frances Fowler                51
    Tom>     9920645 Sarah Watt                      59
    Tom>     9921786 Henry Peterson                72
    Tom>     9925656 William Peterson              62

    Tom> The lines with numbers that begin 99 are records of students taking the
    Tom> course unit (its fairly self-explanatory).

    Tom> I need to extract all the lines with students (ignoring the lines with the
    Tom> teachers and the unit title), and put this into a text file that looks like
    Tom> this:

my %name;
while ( <DATA> ) {
    next unless /\s*(99\d+)\s+(.*)\s+\d+\s*$/;
    $name{$1} = $2;
}
print map "\t$_ $name{$_}\n" => sort keys %name;
__DATA__
A0: Astronomy - Advanced
  S6300 Professor Alethia Williams
    9901770 Cory Peterson                  56
    9911224 Frances Fowler                51
    9920645 Sarah Watt                      59
    9921786 Henry Peterson                72
    9925656 William Peterson              62

output:

        9901770 Cory Peterson                 
        9911224 Frances Fowler               
        9920645 Sarah Watt                     
        9921786 Henry Peterson               
        9925656 William Peterson             

    Tom>     9901770 Cory Peterson
    Tom>     9911224 Frances Fowler
    Tom>     9920645 Sarah Watt
    Tom>     9921786 Henry Peterson
    Tom>     9925656 William Peterson

HTH

--Vinny

<-snip->



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

Date: Fri, 19 Nov 1999 00:09:10 -0000
From: "Tom Clark" <tomclark@btinternet.com>
Subject: Re: Simple help
Message-Id: <8124et$bkk$1@neptunium.btinternet.com>

Hi Vinny,

Thanks for your help.

You know how I asked you all to be gentle...fraid that wasn't quite gentle
enough... ;)

Could you suggest what I should do with that piece of code.  If its
polite.... :)

Thanks again,

Tom

Vincent Murphy <vincent.murphy@cybertrust.gte.com> wrote in message

> my %name;
> while ( <DATA> ) {
>     next unless /\s*(99\d+)\s+(.*)\s+\d+\s*$/;
>     $name{$1} = $2;
> }
> print map "\t$_ $name{$_}\n" => sort keys %name;
> __DATA__
> A0: Astronomy - Advanced
>   S6300 Professor Alethia Williams
>     9901770 Cory Peterson                  56
>     9911224 Frances Fowler                51
>     9920645 Sarah Watt                      59
>     9921786 Henry Peterson                72
>     9925656 William Peterson              62
>
> output:
>
>         9901770 Cory Peterson
>         9911224 Frances Fowler
>         9920645 Sarah Watt
>         9921786 Henry Peterson
>         9925656 William Peterson
>






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

Date: Thu, 18 Nov 1999 16:46:46 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Simple help
Message-Id: <MPG.129e3dd42949ff1d98a234@nntp.hpl.hp.com>

In article <81221a$ak$1@uranium.btinternet.com> on Thu, 18 Nov 1999 
23:27:27 -0000, Tom Clark <tomclark@btinternet.com> says...
> Was wondering if some kind soul could help me.

'Kind' is my middle name (not!).

> Am trying to write a Perl script that will extract data from a text file and
> present it in a new one.
> 
> The (beginning of the) original file from which the data has to extracted
> looks like this:
> 
> A0: Astronomy - Advanced
>   S6300 Professor Alethia Williams
>     9901770 Cory Peterson                  56
>     9911224 Frances Fowler                51
>     9920645 Sarah Watt                      59
>     9921786 Henry Peterson                72
>     9925656 William Peterson              62
> 
> The lines with numbers that begin 99 are records of students taking the
> course unit (its fairly self-explanatory).
> 
> I need to extract all the lines with students (ignoring the lines with the
> teachers and the unit title), and put this into a text file that looks like
> this:
> 
>     9901770 Cory Peterson
>     9911224 Frances Fowler
>     9920645 Sarah Watt
>     9921786 Henry Peterson
>     9925656 William Peterson

I would use a regex instead of all those column numbers.  Read 'perlre' 
or a regex tutorial for explanations.

Here is the code, less all the file manipulations.  (BTW, include $! in 
all those excellent file-open-error diagnostics.)  As you can see, the 
data collection takes exactly one line of code.  :-)


#!/usr/local/bin/perl -w
use strict;

my %students;

/ 99(\d{5}) (.+?)  / and $students{$1} = $2 while <DATA>;

while (my ($id, $name) = each %students) {
    print "99$id\t$name\n";
}
__END__
A0: Astronomy - Advanced
  S6300 Professor Alethia Williams
    9901770 Cory Peterson                  56
    9911224 Frances Fowler                51
    9920645 Sarah Watt                      59
    9921786 Henry Peterson                72
    9925656 William Peterson              62
B1: Bowling - Advanced
  S6300 Professor Jack Nicklaus
    9901770 Cory Peterson                  56
    9911224 Frances Fowler                51
    9920645 Sarah Watt                      59
    9921787 Henry James                   72
    9925656 William Petersen              62

Note that the last time an ID is found is the name that sticks, if they 
are different (Peterson => Petersen).

> Its adapted from another program written by someone else.  I'm no Perl
> genius - as I'm sure is apparent!.  I understand pretty much what's going
> on - its just selecting the right bits of the lines that is eluding me.  The
> file (ultimately) is going to put the students data into an 'insert into'
> command in SQL.

Then presumably you don't care what order the output list is.  As shown, 
it will be essentially random.  But there are ways to sort it, if that 
matters.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 18 Nov 1999 17:04:27 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Simple help
Message-Id: <MPG.129e41f36bf9511098a235@nntp.hpl.hp.com>

In article <xjgd7t7l5zl.fsf@gamora.ndhm.gtegsc.com> on Thu, 18 Nov 1999 
23:44:46 GMT, Vincent Murphy <vincent.murphy@cybertrust.gte.com> says...

 ...

> my %name;
> while ( <DATA> ) {
>     next unless /\s*(99\d+)\s+(.*)\s+\d+\s*$/;
>     $name{$1} = $2;
> }
> print map "\t$_ $name{$_}\n" => sort keys %name;
> __DATA__
> A0: Astronomy - Advanced
>   S6300 Professor Alethia Williams
>     9901770 Cory Peterson                  56
>     9911224 Frances Fowler                51
>     9920645 Sarah Watt                      59
>     9921786 Henry Peterson                72
>     9925656 William Peterson              62

Not quite right, because of all the trailing spaces in your 'names'.  
The greedy regex eats up all the spaces except the final one before the 
grades.

It's often a good idea when debugging something like this to throw a 
visible character such as '|' right before the newline in the print 
statement.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Fri, 19 Nov 1999 08:45:57 -0800
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Simple help
Message-Id: <MPG.129f1e9652ea6e2a98a239@nntp.hpl.hp.com>

In article <3834CED7.6C0D9097@ironhilltech.com> on Fri, 19 Nov 1999 
04:15:19 +0000, Mike Maraist <maraism@ironhilltech.com> says...
> Larry Rosler wrote:

 ...

> > / 99(\d{5}) (.+?)  / and $students{$1} = $2 while <DATA>;
> >
> > while (my ($id, $name) = each %students) {
> >     print "99$id\t$name\n";
> > }

 ...

> This is an almost purely sed / awk type of example.. And as such.. :

No, it is not.  Your code takes no account whatever of winnowing 
duplicates from the output.  You don't imagine I put a hash in there 
just to show that I know how to, do you?

> #!/usr/bin/perl -w -s
> 
> BEGIN {
>    open OUTH, ">file_name.txt";
> }
> 
> /^\s*99(\d+)\s*(.*)\D\d+\s*$/ and print OUTH "99$1\t$2\n";
> 
> # This allows for the case where 99 might appear somewhere on a line other than
> the beginning

No; in fact it requires it to be at the beginning, after optional 
whitespace.  The previous regex would pick it up anywhere, after one 
space.

> #  The last example didn't get rid of the final set of numbers.

Didn't it?  Did you try it to see?  A complete program was posted.  All 
you had to do was to copy and paste it, instead of making stupid 
assertions such as this.

>         This code
> greedily gobbles up the remainder of the line then backtracks past any #
> potential white-space and then all numbers up to the first non number ( which
> will either be a tab or white-space ).

Your regex, in addition to being more verbose, adds no functionality 
and, in fact, erroneously includes trailing spaces in the 'names'.

I fail to see what you have added to this discussion, other than a bunch 
of erroneous statements.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Thu, 18 Nov 1999 23:07:01 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: this pareser won't do 4 visual hebrew
Message-Id: <slrn839j75.12l.tadmc@magna.metronet.com>

On Tue, 16 Nov 1999 05:11:45 +0200, Re'em Bar <reembar@netvision.net.il> wrote:
>since the HTML file I work on is in Visual Hebrew, I can't use the
>standard HTML::Parser.


   And you know that you cannot use it because you tried it, right?

   What went wrong?

   If you tell us, maybe we can help straighten it out.

   Shouldn't matter what kind of data characters are in there,
   the module only pays attention to the markup, not the data.


>all I want is to know how the hell do I exclude --> from my regexp
>search, which is the only problem with my Hebrew parser.


   It is the only problem that you know about.

   That does not imply that there are others that you do not yet
   know about...


>but since this group is more interested in insults then in Perl, 


   Hmmmm.

   If somebody took cuts in front of you in line, would you
   be able to nicely mention to them that the line forms
   to the rear?

   How about if you did tell them nicely, and they took cuts
   again. What would you do.

   Then what would you do when they took cuts a third time?

   And then a fourth?

   You have taken cuts at least that many times. You should expect
   that folks are going to begin to get testy.

   If you don't take cuts, then maybe they won't yell at you.



   Usenet has its own little culture, and you don't know what it
   is. You are committing faux pas left and right. How
   embarrassing for you.

   news.announce.newusers   can save you from that...



---------------------------------------
In article <1995Nov9.193745.13694@netlabs.com>, lwall@netlabs.com (Larry
Wall) wrote: ...

<Larry>  [snip]  I view a programming language as a place to be
<Larry>  explored, like Disneyland. You don't need to have a lot of preparation
<Larry>  to explore a theme park.  You do have to go along with the crowd
<Larry>  control measures, though.  In a sense, each ride has its own
<Larry>  prerequisites--if you cut in line, you risk getting tossed out of the
<Larry>  park.
<Larry> 
<Larry>  What we have here in this newsgroup is a failure in crowd control.
<Larry>  Reading the FAQ is like staying in line--it's something you should
<Larry>  learn in kindergarten.  Usenet needs a better kindergarten.
---------------------------------------


> I'll
>try my luck elsewere.


   Thank you.


-- 
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Thu, 18 Nov 1999 19:34:52 GMT
From: nkaiser@my-deja.com
Subject: Using variables with pattern matching
Message-Id: <811kcc$7mv$1@nnrp1.deja.com>

I'm trying to do something like this:

$from = "(\d+)";
$to = "test $1";
$string = "this is a 25 test.";
$string =~s/$from/$to/;

I would like this to act just as if I did:

$string =~s/(\d+)/test $1/;

How is this possible?


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Fri, 19 Nov 1999 16:52:42 GMT
From: dwiesing@my-deja.com
Subject: Re: WINNT, Email, MS Outlook and MS Exchange
Message-Id: <813v8g$tc9$1@nnrp1.deja.com>

Jason,

I'm running in the same environment as you & ran into similar problems
getting my script to send an Email.  I opted to use NET::SMTP to send
my messages - someone said it's better.  My script isn't quite working
yet because my NT box is soo locked down that I can't find out what our
SMTP address is on our corporate WAN.

If you don't want to switch to SMTP, then maybe a 'perl Makefile.pl' on
your NET directory will help you initialise the missing variable?
(Warning...I'm a newbie at this stuff, so take my advice with a grain
of salt.)

Good luck, & lemme know how it goes.


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Fri, 19 Nov 1999 00:20:55 GMT
From: davidwaters@bigfoot.com (David Waters)
Subject: Writing Modules
Message-Id: <383597dd.2803447@news.lineone.net>

Can anyone give me some tips please on how I go about writing a Perl
module. Most of the standard documentation that I've come across is
useless and doesn't explain the steps required to create a module.

If anyone could point me in the right direction I'd be very grateful.

Thanks alot.

David


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

Date: Thu, 18 Nov 1999 16:54:15 -0800
From: Bill Moseley <moseley@best.com>
Subject: Re: Writing Modules
Message-Id: <MPG.129e3f8fe7e1fe7e989893@nntp1.ba.best.com>

David Waters (davidwaters@bigfoot.com) seems to say...
> Can anyone give me some tips please on how I go about writing a Perl
> module. Most of the standard documentation that I've come across is
> useless and doesn't explain the steps required to create a module.

What part are you having a problem with?

perldoc perlmod has this:
    For example, to start a normal module called Some::Module,
    create a file called Some/Module.pm and start with this
    template:

Follows is a template you can cut-n-paste.  What else would you want?
It tells you that, too:

SEE ALSO
    See the perlmodlib manpage for general style issues related to
    building Perl modules and classes as well as descriptions of the
    standard library and CPAN, the Exporter manpage for how Perl's
    standard import/export mechanism works, the perltoot manpage for
    an in-depth tutorial on creating classes, the perlobj manpage
    for a hard-core reference document on objects, and the perlsub
    manpage for an explanation of functions and scoping.


-- 
Bill Moseley mailto:moseley@best.com
pls note the one line sig, not counting this one.


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 1427
**************************************


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