[19289] in Perl-Users-Digest
Perl-Users Digest, Issue: 1484 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 10 06:10:32 2001
Date: Fri, 10 Aug 2001 03:10:13 -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: <997438212-v10-i1484@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 10 Aug 2001 Volume: 10 Number: 1484
Today's topics:
Re: Simple sorting question (JR)
Re: Simple sorting question (Martien Verbruggen)
Re: Sub that defaults to use $_ in callers context (Anno Siegel)
Re: traversing sub-directories <chuckmg@acm.org>
Use versus Require (diff between using .pl and .pm) (Real Newbie)
Re: Use versus Require (diff between using .pl and .pm) (Eric Bohlman)
Re: Use versus Require (diff between using .pl and .pm) (Martien Verbruggen)
Re: Use versus Require (diff between using .pl and .pm) (wade)
Re: Use versus Require (diff between using .pl and .pm) <brentdax1@earthlink.net>
whitespace flatfile db <andy_yew@iocsasia.com>
Re: whitespace flatfile db (wade)
Re: whitespace flatfile db <samneric@tigerriverOMIT-THIS.com>
Re: Why is $i so popular? <paul@net366.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 9 Aug 2001 21:57:51 -0700
From: tommyumuc@aol.com (JR)
Subject: Re: Simple sorting question
Message-Id: <319333f5.0108092057.37c5a2de@posting.google.com>
Craig Berry <cberry@cinenet.net> wrote in message news:<Xns90F791B069D90cberrycinenetnet1@207.126.101.92>...
> tommyumuc@aol.com (JR) wrote in
> news:319333f5.0108081242.290f501f@posting.google.com:
>
> > I have a tab-delimited file with fields similiar to the ones below:
> >
> > 01 001 00001 23432
> > 01 005 00002 23422
> > 02 003 00001 23421
> > 04 001 00001 23423
> > 08 002 00005 23423
>
> > I need to sort this by the third column so that the output is as
> > follows:
> >
> > 02 003 00001 23421
> > 04 001 00001 23423
> > 01 001 00001 23432
> > 01 005 00002 23422
> > 08 002 00005 23423
>
> Not clear on how you're sorting values which are the same at the third
> column.
I meant column 2 (haven't quite got used to starting my count at 0).
>
> > The only sorting I've ever done to this point was taken care of by
> > @someData = sort { ($a) cmp ($b) } @someData, which worked fine when
> > the column I wanted sorted by were in positions 1 and 2, respectively.
> >
> > How can I sort on the third column in the above examples?
>
> Your sort function block is superfluous, as is the use of parens around $a
> and $b inside it. But anyway:
>
>
> #!/usr/bin/perl -w
> # grtdemo - demo of GR Transform sorting for clpm
> # Craig Berry (20010808)
>
> use strict;
>
> print map { /\d+\t(.*)/s }
> sort
> map { (split)[2] . "\t$_" }
> <DATA>;
>
> __DATA__
> 01 001 00001 23432
> 01 005 00002 23422
> 02 003 00001 23421
> 04 001 00001 23423
> 08 002 00005 23423
This worked perfectly. Thanks for taking the time to help.
------------------------------
Date: Fri, 10 Aug 2001 15:30:34 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Simple sorting question
Message-Id: <slrn9n6sbq.cq.mgjv@martien.heliotrope.home>
On 9 Aug 2001 21:57:51 -0700,
JR <tommyumuc@aol.com> wrote:
> Craig Berry <cberry@cinenet.net> wrote in message news:<Xns90F791B069D90cberrycinenetnet1@207.126.101.92>...
>> tommyumuc@aol.com (JR) wrote in
>> news:319333f5.0108081242.290f501f@posting.google.com:
>>
>> > I need to sort this by the third column so that the output is as
>> > follows:
>> >
>> > 02 003 00001 23421
>> > 04 001 00001 23423
>> > 01 001 00001 23432
>> > 01 005 00002 23422
>> > 08 002 00005 23423
>>
>> Not clear on how you're sorting values which are the same at the third
>> column.
>
> I meant column 2 (haven't quite got used to starting my count at 0).
Column 2 is the third column. I think that Craig was asking you how you
wanted the rows sorted that have identical values in that column
column2, the third column), i.e. the first three rows in the (sorted)
example you give. AFAICT there is nothing in there that is sorted at
all. You haven't preserved the ordering of the original, and you haven't
sorted it by either one of the three other columns. Does this mean that
you simply don't care, or is there some other scheme that determines the
order of those three?
> This worked perfectly. Thanks for taking the time to help.
Looks like you don't care about that order, right? :)
Martien
--
Martien Verbruggen |
Interactive Media Division | Useful Statistic: 75% of the people
Commercial Dynamics Pty. Ltd. | make up 3/4 of the population.
NSW, Australia |
------------------------------
Date: 10 Aug 2001 09:44:29 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Sub that defaults to use $_ in callers context
Message-Id: <9l0adt$abe$1@mamenchi.zrz.TU-Berlin.DE>
According to Michael Carman <mjcarman@home.com>:
> Yves Orton wrote:
[making $_ a default sub parameter]
> Of course, if you want to be able to pass in false values, you should
> change that to:
>
> sub foo {
> my $param = (defined $_[0]) ? $_[0] : $_;
> #...
> }
This still doesn't let you pass "undef" explicitly as in foo( undef).
This is more general:
my $param = @_ ? shift : $_;
Anno
------------------------------
Date: Fri, 10 Aug 2001 04:11:32 GMT
From: Chuck Goldstein <chuckmg@acm.org>
Subject: Re: traversing sub-directories
Message-Id: <3B735F35.8AECD974@acm.org>
Many thanks, guys. Really appreciate the input. It's given me a
real push. Am also an old SGML'r, Tad.
Chuck
Tad McClellan wrote:
> Chuck Goldstein <chuckmg@acm.org> wrote:
>
> >Am new to Perl. An old (more ways than one) awk-user.
>
> Note that installed along with perl itself is a program
> named "a2p" (awk to perl) that translates awk code into Perl.
>
> So when you know how to do it in awk, a2p can show you how
> to do it in Perl.
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
--
Chuck
Charles M. Goldstein
10751 Durland Ave NE (206)362-2999
Seattle, WA 98125 chuckmg@acm.org
------------------------------
Date: 9 Aug 2001 23:05:36 -0700
From: realnewbie2000@yahoo.com (Real Newbie)
Subject: Use versus Require (diff between using .pl and .pm)
Message-Id: <a2b38b4d.0108092205.10aba675@posting.google.com>
I am new to Perl and would like to develop using blocks of functionality.
How can I create and use functions that can be reused?
Some queries related to this are:
1. Should I create a .pl file containing the functions and then use the
pl2pm utility to convert it into a .pm?
2. Should I use "require Utils.pl" or "Use Utils.pm" and what's the
difference?
Regards,
Newbie
------------------------------
Date: 10 Aug 2001 08:04:36 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Use versus Require (diff between using .pl and .pm)
Message-Id: <9l04ik$qqv$1@bob.news.rcn.net>
Real Newbie <realnewbie2000@yahoo.com> wrote:
> I am new to Perl and would like to develop using blocks of functionality.
> How can I create and use functions that can be reused?
> Some queries related to this are:
> 1. Should I create a .pl file containing the functions and then use the
> pl2pm utility to convert it into a .pm?
pl2pm is intended for converting libraries that were written under Perl 4
to use the new module features added in Perl 5. It would be pointless to
use it the way you're proposing; just learn about modules (perldoc
perlmod) and write them.
> 2. Should I use "require Utils.pl" or "Use Utils.pm" and what's the
> difference?
perldoc -f use
perldoc -f require
perldoc perlmod
------------------------------
Date: Fri, 10 Aug 2001 18:29:15 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Use versus Require (diff between using .pl and .pm)
Message-Id: <slrn9n76qq.cq.mgjv@martien.heliotrope.home>
On 9 Aug 2001 23:05:36 -0700,
Real Newbie <realnewbie2000@yahoo.com> wrote:
> I am new to Perl and would like to develop using blocks of functionality.
> How can I create and use functions that can be reused?
if you only have one function:
You write the function, put it in a file somewhere, and require it.
If you have a group of related functions:
You put the functions in a perl library (deprecated) or module
(preferred), and require or use it.
if you have a group of related functions that you are going to usea lot,
possibly on different machines:
Create a decent module distribution starting off with with h2xs, and if
you feel it's worth it, share your code with the world, and upload it to
CPAN.
If you have a large amount of code that could be implemented as objects:
Write an OO module/class.
> Some queries related to this are:
>
> 1. Should I create a .pl file containing the functions and then use the
> pl2pm utility to convert it into a .pm?
No. pl2pm is only intended to change old perl 4 libraries to perl5
modules, and only then when you intend to start using perl5 specific
features. You should write modules directly.
That is, if the functionality you are writing sort of fits together as a
group, then you should create a library. For collections of tools and
utilities, I'd probably just put them in small perl libraries in a
directory somewhere, and require them when needed. For 'blocks of
functionality' as you say, I'd create a module. There isn't that much
difference between libraries and modules.
the perlmod documentation has more info. You definitely want to read
perlnewmod. You probably also want to read perlmodlib, maybe the
Exporter documentation, and maybe perltoot if you are thinking of making
this functionality available via a OO interface. perlsub is also a good
one to look at. Also have a look at h2xs, which seems like it's only
usable for C stuff, but in reality it can be used quite well to set up a
stub implementation of your module (see perlnewmod).
> 2. Should I use "require Utils.pl" or "Use Utils.pm" and what's the
> difference?
This is a FAQ.
$ perldoc -q "require.*use"
Found in /opt/perl/lib/5.6.1/pod/perlfaq8.pod
What's the difference between require and use?
You also will want to read the entries for require and use in the
perlfunc documentation
You should also pick a much better name for your module than that. Avoid
conflict with anything already on CPAN, for example. If the code is
specific to you, or your company, reflect that in the name. If the code
fits in one of the CPAN categories, use that. If you ever plan to upload
it to CPAN, you'll have a lot of trouble if you need to rename all of
your stuff. Or if you ever install someone else's modules and they have
also called in Utils.pm, you're in trouble again. Just pick a sensible
name.
> Newbie
In the future, please use your real name, or at least something that
isn't as self-deprecating.
Martien
PS. For the documentation: use perldoc. man perldoc if you're on Unix,
perldoc perldoc if you're not (use Shuck if you're on a Mac).
--
Martien Verbruggen |
Interactive Media Division | Think of the average person. Half of
Commercial Dynamics Pty. Ltd. | the people out there are dumber.
NSW, Australia |
------------------------------
Date: 10 Aug 2001 02:06:00 -0700
From: jjchen@alumni.ice.ntnu.edu.tw (wade)
Subject: Re: Use versus Require (diff between using .pl and .pm)
Message-Id: <4259465b.0108100106.4aa6a8a1@posting.google.com>
realnewbie2000@yahoo.com (Real Newbie) wrote in message news:<a2b38b4d.0108092205.10aba675@posting.google.com>...
> I am new to Perl and would like to develop using blocks of functionality.
> How can I create and use functions that can be reused?
>
> Some queries related to this are:
>
> 1. Should I create a .pl file containing the functions and then use the
> pl2pm utility to convert it into a .pm?
> 2. Should I use "require Utils.pl" or "Use Utils.pm" and what's the
> difference?
>
> Regards,
> Newbie
As I know, .pl format is just for perl 4,
It means pl2pm is transfer perl4 format to perl5 format.
So, please refer to camel book 2/e or 3/e.
After you writing an module, such as Utils.pm,
"use Utils;" is enough, maybe you need declare "my $u =
$Utils->new();" further if it is an object.
or "perldoc -q module", "perldoc -q object" in advance.
------------------------------
Date: Fri, 10 Aug 2001 09:58:03 GMT
From: "Brent Dax" <brentdax1@earthlink.net>
Subject: Re: Use versus Require (diff between using .pl and .pm)
Message-Id: <LeOc7.2540$2M3.235716@newsread1.prod.itd.earthlink.net>
"Real Newbie" <realnewbie2000@yahoo.com> wrote in message
news:a2b38b4d.0108092205.10aba675@posting.google.com...
> I am new to Perl and would like to develop using blocks of functionality.
> How can I create and use functions that can be reused?
As you apparently guessed, you should write a module.
> Some queries related to this are:
>
> 1. Should I create a .pl file containing the functions and then use the
> pl2pm utility to convert it into a .pm?
No; pl2pm is intended to convert old Perl 4 code into Perl 5 code.
> 2. Should I use "require Utils.pl" or "Use Utils.pm" and what's the
> difference?
Require happens at runtime; use happens at compile-time. The big difference
is this:
print "foo";
use Some::Module::That::Doesn't::Exist; #yes, that's valid
__END__
Can't locate Some/Module/That/Doesn/t/Exist.pm in @INC (@INC contains:
C:/Perl/lib C:/Perl/site/lib .).
BEGIN failed--compilation aborted.
versus
print "foo\n";
require Some::Module::That::Doesn't::Exist;
__END__
foo
Can't locate Some/Module/That/Doesn/t/Exist.pm in @INC (@INC contains:
C:/Perl/lib C:/Perl/site/lib .) at - line 2.
See the difference? The use aborted the program before it even started, but
the require allowed the stuff that came before it to run!
You generally want to use use, unless you can't figure out what module
you'll need until you're already running; in that case, you'll want require.
See the documentation on use and require in perlfunc for more information on
this.
BTW, this was in one of the perlfaqs. You could have saved me the time to
type this and you the time waiting for an answer if you'd looked around a
little more before heading to Google Groups.
HTH,
--Brent Dax
brentdax1@earthlink.net
------------------------------
Date: Fri, 10 Aug 2001 14:08:40 +0800
From: "Yew" <andy_yew@iocsasia.com>
Subject: whitespace flatfile db
Message-Id: <9kvte5$rr$1@coco.singnet.com.sg>
Sorry, but this is a newbie question. I've with me a flat file that stores
the data along columns separated by spaces only (not tab limited) and would
like to be able to run queries on it. Is it possible to find scripts that
can read such files in the first place? Most either need explicit tabs or
comma's in place to be able to read it properly. It isn't timing critical,
just want something to read it up first before I do some group manipulations
on it.
Thanks.
------------------------------
Date: 10 Aug 2001 02:17:10 -0700
From: jjchen@alumni.ice.ntnu.edu.tw (wade)
Subject: Re: whitespace flatfile db
Message-Id: <4259465b.0108100117.6b0b6414@posting.google.com>
"Yew" <andy_yew@iocsasia.com> wrote in message news:<9kvte5$rr$1@coco.singnet.com.sg>...
> Sorry, but this is a newbie question. I've with me a flat file that stores
> the data along columns separated by spaces only (not tab limited) and would
> like to be able to run queries on it. Is it possible to find scripts that
> can read such files in the first place? Most either need explicit tabs or
> comma's in place to be able to read it properly. It isn't timing critical,
> just want something to read it up first before I do some group manipulations
> on it.
>
> Thanks.
You can do it by yourself simply.
while (<>) {
chomp;
my @datas = split /\s+/;
}
After you accessing it, write back.
or, replace "whitespace" to ":", you can use "DBM".
Please refer to "perldoc -f dbmopen"
maybe refer "tie" further more...
------------------------------
Date: Fri, 10 Aug 2001 05:56:08 -0400
From: Samneric <samneric@tigerriverOMIT-THIS.com>
Subject: Re: whitespace flatfile db
Message-Id: <3B73AFB8.A7E7C7B8@tigerriverOMIT-THIS.com>
wade wrote:
> "Yew" <andy_yew@iocsasia.com> wrote in message news:<9kvte5$rr$1@coco.singnet.com.sg>...
> > I've with me a flat file that stores the data along columns
> > separated by spaces only (not tab limited)
> while (<>) {
> chomp;
> my @datas = split /\s+/;
> }
"split /\s+/" is wrong here, since it clobbers blank fields.
You mean: "split / /" - where the line splits on the actual separating
character.
And @datas better be used in the while loop :)
------------------------------
Date: Fri, 10 Aug 2001 08:47:45 +0100
From: "Paul Fortescue" <paul@net366.com>
Subject: Re: Why is $i so popular?
Message-Id: <997429593.24383.0.nnrp-13.d4f094e4@news.demon.co.uk>
"Lou Moran" <lmoran@wtsg.com> wrote in message
news:oep5ntcvi3fduk1dn895kdv3l60ks7gsjf@4ax.com...
> Why is $i such a popular variable?
>
> Is there any significance to it, or is it a foo bar sort of thing?
> Just wondering.
>
>
From an old boy ...
I started on fortran and algol 68, and that indeed is why $i is so popular.
$j, $k ditto. $x would be used for the same sort of reason, as would $y (for
'real' numbers, not integers. And indeed $str, $temp, $garbage so you could
find them easily when taking the working comments out of your code when you
issued it.
I don't however, know why foo and bar exist, I was already past it by then!
------------------------------
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 1484
***************************************