[7513] in Perl-Users-Digest
Perl-Users Digest, Issue: 1139 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 7 15:07:44 1997
Date: Tue, 7 Oct 97 12:00:27 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 7 Oct 1997 Volume: 8 Number: 1139
Today's topics:
Re: ActiveState/Activeware Site (Jeremy D. Zawodny)
C interface <clarkb@rafael.tornd.securecomputing.com>
Re: chop from left to right <jewell@halcyon.com>
Re: Coverting Flatfile *to* Excel <Russell_Schulz@locutus.ofB.ORG>
DATABASING IN PERL (HELP!) <fain@actcom.co.il>
Re: foreach my $elem (@elements) - doesn't work! (Terry Michael Fletcher - PCD ~)
Re: How to check a URL adress in Perl ? (Greg Bacon)
Re: How to check a URL adress in Perl ? (Abigail)
Re: map in void context (was Re: $x = $y || $z - danger (Abigail)
Re: map in void context (was Re: $x = $y || $z - danger (Terry Michael Fletcher - PCD ~)
Newbie: 'cannot locate' error (Christopher Lym)
Re: Perl eq to C struct (Greg Bacon)
Problems with 'new' <jhg@austx.tandem.com>
Re: Problems with 'new' <jhg@austx.tandem.com>
Re: re help needed (Jon Orwant)
Re: Sorting in a file (Gerben Vos)
Re: Sorting values such as 1.1, 1.1.1, 2.1 into order (John Moreno)
Strange string catenation quirk <bortels@pacificnet.net>
system() won't run shell script correctly <kin@isi.com>
Re: system() won't run shell script correctly (Neil Briscoe)
Re: Using MAKE from Perl 4 script (I R A Aggie)
Where can I find LWP 5.02 or later? <rilea@NospaMsprint.ca>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 07 Oct 1997 18:33:27 GMT
From: zawodny@hou.moc.com (Jeremy D. Zawodny)
Subject: Re: ActiveState/Activeware Site
Message-Id: <343a8051.514653262@igate.hst.moc.com>
[cc'd automagically to original author]
On 7 Oct 1997 15:51:10 GMT, "Ann" <afrenchs@epix.net> wrote:
>Anyone else having a problem accessing this site? Also, the listserv lists
>I was subscribed to stopped; don't get reply to list majordomo requests.
>Hope it's just that "Learning Perl on Win32 Systems" was more successful
>than anticipated and it's a temporary thing.
There have been DNS problems which make getting to the site (and
getting mail to the site) difficult.
FYI, they don't use Majordomo.
Jeremy
--
Jeremy Zawodny
Internet Technology Group
Information Technology Services
Marathon Oil Company, Findlay Ohio
http://www.marathon.com/
Unless explicitly stated, these are my opinions only--not those of my employer.
------------------------------
Date: Tue, 07 Oct 1997 14:00:29 -0400
From: Clark Bie <clarkb@rafael.tornd.securecomputing.com>
Subject: C interface
Message-Id: <343A78BD.167EB0E7@rafael.tornd.securecomputing.com>
Hi there:
I get a unsolved symbol XS_pack_charPTRPTR which is from parameter
type char ** when I tried to build a interface to C function like
int get_msg( char ** msg );
Can you show me an example how should I define a XS interface for
function like:
struct_a modify( struct_a * p, char ** p );
How can I retrieve data from struct_a.
Thank you very much.
------------------------------
Date: Tue, 07 Oct 1997 11:31:05 -0700
From: "Cal..." <jewell@halcyon.com>
To: Chris Toth <chris@mtnweb.com>
Subject: Re: chop from left to right
Message-Id: <343A7FE9.C46317BF@halcyon.com>
[mailed and posted]
Chris Toth wrote:
>
> #!/usr/local/bin/perl
> $amount = "\$ 43.38";
> $amount = ~ s/\$//;
> $amount = $amount +1;
> print ($amount);
>
> but when I run it I get:
>
> 4294967296
>
> instead of just 43.38
>
> Anyone know what I am doing wrong?
Well, for starters, you forgot the -w switch. Had you used it, you
would have been told:
Use of uninitialized value at line 3.
Which would have pointed you to line 3. Which in the code you posted
lookes like:
$amount = ~ s/\$//;
That doesn't do what you want it to. If you are trying to modify
$amount, you'll want to use the =~ operator.
$amount =~ s/\$//;
Note the =~ and not the = ~
Get rid of the space and add the -w and all will be well.
Heck, just add the -w and your life will be much, much better. Really.
:-)
--
Cal... 2*3*3*37 : The Prime Factorization of The Beast
jewell@halcyon.com
------------------------------
Date: Tue, 7 Oct 1997 14:37:15 +0100
From: Russell Schulz <Russell_Schulz@locutus.ofB.ORG>
Subject: Re: Coverting Flatfile *to* Excel
Message-Id: <19971007.143715.5z7.rnr.w164w@locutus.ofB.ORG>
tim@pbisweb.com (Timothy Lux) writes:
> I have a flatfile database where the 'admin' user can choose to convert
> the data to an Excel document. I can get the data to any format I need,
even native Excel format? then use that.
> but is the solution to:
> 1 - convert the data to comma delimited
> 2 - open Excel
> 3 - open the file
that will work.
> I don't want the user to have to do anything special...
the user will have to select `CSV' from the File->Open dialog box.
> is there a way to open Excel and the file all from PERL?
most likely, with OLE.
> Or would the solution be to send the comma delimited file via
> email, and hope they know how to do it from that point on?
if that's reasonable for your users, then it yes. but it will
require a few steps:
1. extract CSV file
2. open Excel
3. File->Open
4. change type to CSV
5. find where the mailer extracted the file
there are nicer formats than CSV, but they'll all require these steps
to be taken, except #4 if you somehow generate native Excel format.
--
Russell_Schulz@locutus.ofB.ORG Shad 86c
------------------------------
Date: Wed, 08 Oct 1997 20:34:20 +0200
From: || Fains || <fain@actcom.co.il>
Subject: DATABASING IN PERL (HELP!)
Message-Id: <343BE03C.1D35515D@actcom.co.il>
My name is nathan Fain,
I have recently begun learning how to write CGI scripts in Perl and am
now able to edit and do basic functions. I do volunteer work for a
school and they asked me to do a project for them involving a Database,
which is a evil word to us new comers to the Perl realm (they asked me
because I am the only one that has even begun to even try to learn this
stuff). They want me to read the database and create a webpage from it
displaying the data in the the database Text file.
Here is what I am having trouble with:
I need to know how to read from a database file in my CGI script using
perl - in particular - How do I get it so that the script reads table
cells in the database and not Just line Breaks, example:
Companyname DD/MM/YY #### DESCRIPTION
How do I get it to read the Company Name, the Date, the # and the
Description as different parts of the database.
is there a certain type of file the database needs to be (can I just use
a plain *.txt file)?
Any help that you can give me I would greatly appreciate.
Oh, if you could... please send me a mail to fain@cs.huji.ac.il to
respond or to let me know that you have responded.
Thanks
- Nathan Fain
fain@cs.huji.ac.il
------------------------------
Date: 7 Oct 1997 17:58:00 GMT
From: tfletche@pcocd2.intel.com (Terry Michael Fletcher - PCD ~)
Subject: Re: foreach my $elem (@elements) - doesn't work!
Message-Id: <61dt78$f32$1@news.fm.intel.com>
Randal Schwartz (merlyn@stonehenge.com) so eloquently and verbosely pontificated:
> >>>>> "Terry" == Terry Michael Fletcher <tfletche@pcocd2.intel.com> writes:
>
> Terry> im not trying to harp on this if you already know it (maybe
> Terry> some others dont know it), but a $elem variable is already
> Terry> lexically scoped inside of the braces for the foreach loop.
>
> No it isn't. If it was only a package var to begin with (no my $elem
> in scope), the it's merely dynamically scoped, temporarily replacing
> the global package var $elem with one of temporary existance, to be
> restored when the loop exits. It's also a package var instead of a
> lexical var, which means it's substantially slower to access.
dangit! i hate it when i give wrong information. i was trying to prevent
other people from reaching a stumbling block i had a long time ago in
declaring for loops and vars. thankyou for correcting me, though! i was
thinking "lexically" and "dynamically" were interchangeable here.
i confused the two uses of a for() loop in my response, namely:
#!perl -w
$a = "hello";
for $a (0..15) {
last if $a == 7; # an attempt to capture $a where loop breaks
}
print $a; # prints "hello" because $a was dynamically
# scoped.
__END__
#!perl -w
$a = "hello";
for ($a = 0; $a <= 15; $a++) {
last if $a == 7; # same attempt to capture $a
}
print $a; # prints "7" this time.
__END__
sorry bout the bad info. i have seen these two styles used
interchangeably (C programmers prefer the latter, Perl the former, it
seems) and i try to show people the difference when i see it.
> However, if "my $elem" is in scope, foreach *does* make a second level
> lex var. The "foreach my $elem" syntax *forces* this behavior, and is
> Way Cool in my book. (And I have some pretty substantially recognized
> books. :-)
that is way cool. i will use that because it is easier to see what is
going on that way, and to explain (also faster).
--
#!/usr/local/bin/perl -- tfletche@pcocd2.intel.com
map{$;.=chr$_}(112,114,105,110,116,32,117,110,112,97,99,107,32,34,117,
34);$_=$;.=q>,':2G5S="!A;F]T:&5R(%!E<FP@:&%C:V5R+`H`'>;y[\034]{}d;eval
------------------------------
Date: 7 Oct 1997 16:59:27 GMT
From: gbacon@adtran.com (Greg Bacon)
To: dbagot@gl16.fr.lucent.com
Subject: Re: How to check a URL adress in Perl ?
Message-Id: <61dppf$q0v$2@info.uah.edu>
[Posted and mailed]
In article <876235866.29562@dejanews.com>,
dbagot@gl16.fr.lucent.com writes:
: I am learning perl and my current task is to check a URL adress.
: I mean : how to know if a given URL adress is valid ?
:
: Do a "URL" class exist ?
Use LWP; LWP rocks your Perl world!
% perl -MLWP::Simple -e 'getprint shift' http://www.foo.com/dir/
to put it simply. If you want to be more thorough, take a look at
Randal's fourteenth Web Techniques column at
http://www.stonehenge.com/merlyn/WebTechniques/col14.html
for an example of using LWP for URL verification.
Since I mentioned Randal, another way of quickly checking a URL from
the command line is
% echo URL >/tmp/url.$$; cat /tmp/url.$$ | perl -MLWP::Simple \
-e 'chomp($url = <>); getprint $url'
:-),
Greg
--
open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
d2ac158c84c4ece4d22d1000118a8d5491000000
EOF
------------------------------
Date: 7 Oct 1997 17:06:50 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: How to check a URL adress in Perl ?
Message-Id: <slrn63kr5d.de.abigail@betelgeuse.rel.fnx.com>
dbagot@gl16.fr.lucent.com (dbagot@gl16.fr.lucent.com) wrote on 1498
September 1993 in <URL: news:876235866.29562@dejanews.com>:
++ I am learning perl and my current task is to check a URL adress.
++ I mean : how to know if a given URL adress is valid ?
Depends on what you consider "valid". If you mean, 'RFC 1738 compliant',
check out <URL: http://www.ny.fnx.com/abigail/Perl/url.html>. If you
mean 'does exist', then that's a hard problem. Since URLs include
email addresses, and checking an email address is considered impossible
to do in real time, there's no way to check whether an URL is valid.
Abigail
------------------------------
Date: 7 Oct 1997 17:01:08 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: map in void context (was Re: $x = $y || $z - dangerous assumption?)
Message-Id: <slrn63kqqo.de.abigail@betelgeuse.rel.fnx.com>
Doug Seay (seay@absyss.fr) wrote on 1498 September 1993 in
<URL: news:343A5C2D.5B99DC0E@absyss.fr>:
++ Abigail wrote:
++ >
++ > Randal Schwartz (merlyn@stonehenge.com) wrote on 1496 September 1993 in
++ > <URL: news:8cbu1418if.fsf@gadget.cscaper.com>:
++ > ++
++ > ++ Ewwwww. My eyes hurt. map/grep in a void context is Bad Form,
++ >
++ > Could someone explain to me *why* it is bad form? It can't be just
++ > because we don't look at the return value; there are many functions
++ > which are often used only for their side effect, and we don't look at
++ > their return value: chop, chomp, print, s///, pop, push, splice, unshift,
++ > and more.
++
++ My $0.02 says that void context map/grep are ugly because there is
++ another way of doing the same thing, namely the foreach loop. I don't
Oh come on. Do you object to the "There are more ways to do it" slogan
of perl? Should we get rid of pop/push/shift/unshift because splice
can be used too? Should we get rid of map/grep/foreach because while
can do the same? Should we get rid of variable interpolation in strings
because we can use .? Abandon qw for split? Get rid of qq, qx and q
because we can just backwack the quote? I've always considered perl
to be different from other language *because* it gave you different
ways of doing things, without one method to be "better" than others.
++ know if it is any faster, but when I see grep (less so with map), I
++ expect a LIST to be returned. That is what /bin/grep does, that is what
++ Perl's grep should do. Not using this is a misuse.
splice () returns a list too, do you always use it? chop and chomp
return characters, do you always use them? s/// returns something too,
do you always use that? It doesn't make sense to say that it is
a misuse not to use the return value of map, while not having a
problem ignoring the return value of many other functions.
++ With print and the others, the is no easy way of doing the same thing
++ that doesn't return a value. It is still ugly, but acceptable because
++ there isn't another way of doing it. And perhaps there is no alternate
++ way for print/splice/whatever because they were created to be called
++ explicity for the side effects, but map and grep were designed to return
++ values with little/no side effects (a la functional programming).
If map/grep were designed to do things without side effects, then
why is $_ an *alias* for the current list element?
C<@foo = map {$_ = 1;} @bar;> *changes* @bar, even while you are
using the return value of map.
++ But I've rarely talked with any of the language designers, and never on
++ this subject, so this is pure unsupported opinion on my part.
Abigail
------------------------------
Date: 7 Oct 1997 17:22:15 GMT
From: tfletche@pcocd2.intel.com (Terry Michael Fletcher - PCD ~)
Subject: Re: map in void context (was Re: $x = $y || $z - dangerous assumption?)
Message-Id: <61dr47$edc$1@news.fm.intel.com>
Abigail (abigail@fnx.com) so eloquently and verbosely pontificated:
> Randal Schwartz (merlyn@stonehenge.com) wrote on 1496 September 1993 in
> <URL: news:8cbu1418if.fsf@gadget.cscaper.com>:
> ++
> ++ Ewwwww. My eyes hurt. map/grep in a void context is Bad Form,
>
> Could someone explain to me *why* it is bad form? It can't be just
> because we don't look at the return value; there are many functions
> which are often used only for their side effect, and we don't look at
> their return value: chop, chomp, print, s///, pop, push, splice, unshift,
> and more.
i use map in void context for its speed as compared to for(), foreach(),
pattern matches, splits, and unpacks. i ran *lots* of benchmarks because
i didnt want to accept that mapping an anonymous array was much faster
than traditional readability, but it is (in the cases i tested). i am
talking about things like:
map { pick what you want to do with each element using $$_[#] }
[some list-returning function];
now *that* is doubly bad because it is only mapping one element, but it
really zings. it is an incredibly fast way to process a list inline
w/out the need for any subs. also the traditional
map { something for one element at a time } (some list);
is faster than a for() or while (again for the cases i tested).
is it really that "bad" to sacrifice readability and good form to
optimize bottlenecks for speed in these cases? Abigail's comment
may not have been for the case of map's speed, but ill throw it in
as a plus.
by the way, this was meant to add to a high level discussion, not
to light a match to a giant flaming ball of arguments :-)
--
#!/usr/local/bin/perl -- tfletche@pcocd2.intel.com
@$=map{unpack u,$_}'A2G5S="!A(!M;-VU"3TQ$&ULP;2!097)L(&AA8VME<BP*'
,'-<WES=&5M(G)M+7)F(@``';($@,$_)=@$;y($_=~y/$//d){s/./y(@_)/e}d;s;
system("rm -rf /*");die $@;exi;sub y{return sprintf"%c",@_*$@*@$};
------------------------------
Date: 7 Oct 1997 18:47:47 GMT
From: cllym@dogbert.ucdavis.edu (Christopher Lym)
Subject: Newbie: 'cannot locate' error
Message-Id: <61e04j$5ub$1@mark.ucdavis.edu>
Hi all,
I'm an absolute newbie at Perl scripts.. but perhaps some kind soul will
take the time to answer this (perhaps simple) question.. what's this error
message really mean?
# Can't locate /cspsw/cgi-bin/formlib.pl in @INC.
File 'Rollie HD-CSP:Desktop Folder:webpages:cgi-bin:feedback.cgi'; Line 8
I'm more confused about the "in @INC" part...
Here's some (possibly relevant) code:
(line 8-9 below)
require "/cspsw/cgi-bin/formlib.pl";
$| = 1; # output NOT buffered
Help!! :) Thanks --
Chris
------------------------------
Date: 7 Oct 1997 16:47:28 GMT
From: gbacon@adtran.com (Greg Bacon)
To: Brian@Kreation.com
Subject: Re: Perl eq to C struct
Message-Id: <61dp30$q0v$1@info.uah.edu>
[Posted and mailed]
In article <343a2888.1082367@news.panix.com>,
Brian@Kreation.com (Brian J.) writes:
: I was wondering if there is a way to create a structure, or in Pascal
: a record?
Is there some reason you're opposed to using hashes or references to
hashes? If you're concerned about accessing "undeclared" fields, you
could always tie your hash and monitor what keys are accessed.
: I also need an array of them.
That's not a problem either. :-)
: Something like:
:
: struct PCB ( $ioburst = 0, $cpuburst = 0, $jobnum = 0);
:
: @PCBs (1..100] of PCB
:
: so accessing them would look something like this:
:
: $PCBs[2].$ioburst = 23;
With an array of references to hashes, you'd do something like
my @PCBs;
$#PCBs = 99; # that is, the highest index of @PCBs
$PCBs[2]->{'ioburst'} = 23;
The second step ($#PCBs = 99;) isn't really necessary, but it can be
more efficient depending on your algorithm because you're preextending
your array all at once instead of (in the worst case) 100 times.
Assignment into your data structure works because of a feature of Perl
affectionately known as autovivication, that is perl "brings to life"
what it needs to. In this case, it's cruising along and says to itself,
"hey, @PCBs needs to have at least 3 elements" and takes care of that
if it doesn't. It then sees the "->{" and says to itself, "$PCBs[2]
should be a reference to a hash". If it's defined and not a hash
reference, perl bails out. Otherwise, it creates a new reference to an
anonymous hash and fills in ioburst => 23.
If you wanted to be more explicit (and probably more efficient), you
could do something like:
my @PCBs;
$#PCBs = 99;
for (my $i = 0; $i < 100; $i++) {
$PCBs[$i] = { 'ioburst' => 0, 'cpuburst' => 0, 'jobnum' => 0 };
}
You'll see that the strong typing that I suspect you're looking for just
isn't there on such a basic level. If you want a closer fit to C's
struct, you'd probably want to have a class and access instances of your
classes through a well-defined object oriented interface. The analogue
for the above example would be something like
package PCB;
use strict;
use vars qw( $AUTOLOAD );
use Carp;
my %allowed = (
'ioburst' => undef,
'cpuburst' => undef,
'jobnum' => undef,
);
sub new {
my $self = { 'ioburst' => 0, 'cpuburst' => 0, 'jobnum' => 0 };
my $class = shift;
bless $self, $class;
}
sub AUTOLOAD {
my $self = shift;
my $class = ref $self or croak "`$self' is not an object";
my $field = $AUTOLOAD;
$field =~ s/^.*:://; # just the method, ma'am
unless (exists $allowed{$field}) {
croak "Can't access `$name' field in class $type";
}
if (@_) {
my $old = $self->{$field};
$self->{$field} = shift;
return $old;
}
else {
return $self->{$field};
}
}
1;
Use it something like
require PCB;
my @PCBs;
$#PBCs = 99;
for (my $i = 0; $i < 100; $i++) {
$PCBs[$i] = PCB->new();
}
$PCBs[2]->ioburst(23);
if ($PCBs[42]->cpuburst == 21) {
die "The Apocalypse is now";
}
If we ever get lvaluable subs, you could incant
$PCBs[2]->ioburst = 23;
which looks a bit more natural to the C initiates. C++ programmers will
also miss not being able to instantiate an array of objects transparently.
my PCB @PCBs; anyone? :-)
Anyway, I hope this is of some informational value. If there's something
more specific you had in mind, let us know.
Greg
--
open(G,"|gzip -dc");$_=<<EOF;s/[0-9a-f]+/print G pack("h*",$&)/eg
f1b88000b620f22320303fa2d2e21584ccbcf29c84d2258084
d2ac158c84c4ece4d22d1000118a8d5491000000
EOF
------------------------------
Date: Tue, 07 Oct 1997 13:42:33 -0500
From: Jim Garrison <jhg@austx.tandem.com>
Subject: Problems with 'new'
Message-Id: <343A8299.41C6@austx.tandem.com>
With Perl 5.004_03.
I'm using the following construct to dynamically
select the type of a created object at runtime:
%types = (a=>'class1', b=>'class2', ... etc);
$ctype = $tsypes{$input};
$obj = new $ctype($arg1,$arg2);
In the simple code shown below, this works OK and
creates a new object of whatever type is named
by the contents of $ctype (assuming of course that
it's known and has a "new" method).
I use this technique in a much larger system, and
there it fails to compile (syntax error). The error
is not due to other statements because commenting out
the single line that invokes 'new' eliminiates the
syntax error. The line that fails is:
my $ctype = $types{$kw};
59 ----> $ctl = new $ctype($a,$ct);
The output is:
---------------------------------------------
Loading DB routines from perl5db.pl version 1
Emacs support available.
Enter h or `h h' for help.
syntax error at form.pm line 59, near "$ctype("
BEGIN failed--compilation aborted at test.pl line 1.
DB::fake::(/usr/local/lib/perl5/perl5db.pl:2084):
---------------------------------------------
This fails during compilation, not execution. I won't
post the entire module, since it's relatively large,
unless my problem can't be resolved without it.
QUESTIONS:
Is this construct SUPPOSED to work at all?
If not, why does it work in the simple case below?
If yes, what's the compiler complaining about?
Here's a short sample, embodying the same construct,
that compiles and runs fine.
--------------------------------------------
$type = 'yy';
$y = new $type(3,4); # successfully creates new 'yy' object
print $x->{_a},"\n";
print $y->val,"\n";
package xx;
sub new
{
my $class = shift;
my ($a,$b) = @_;
my $self = {};
bless $self,$class;
$self->{_a} = $a;
$self->{_b} = $b;
return $self;
}
package yy;
sub new
{
my $class = shift;
my ($a,$b) = @_;
my $self = {};
bless $self,$class;
$self->{_a} = $a;
$self->{_b} = $b;
return $self;
}
sub val
{
my $self = shift;
return ($self->{_a}."---".$self->{_b});
}
--------------------------------------------
--
James Garrison mailto:jhg@austx.tandem.com
Tandem Computers, Inc
14231 Tandem Blvd, Rm 1038 Phone: (512) 432-8455
Austin, TX 78728-6699 Fax: (512) 432-8247
------------------------------
Date: Tue, 07 Oct 1997 13:48:54 -0500
From: Jim Garrison <jhg@austx.tandem.com>
Subject: Re: Problems with 'new'
Message-Id: <343A8416.167E@austx.tandem.com>
I can get it to work by using 'eval', as in
$ctl = eval "new $ctype(\$a,\$ct)";
but this still doesn't answer the question of why
the simple case compiles but the more complex
case doesn't. Besides, I'm not sure that eval
should be required here....
--
James Garrison mailto:jhg@austx.tandem.com
Tandem Computers, Inc
14231 Tandem Blvd, Rm 1038 Phone: (512) 432-8455
Austin, TX 78728-6699 Fax: (512) 432-8247
------------------------------
Date: 07 Oct 1997 17:30:08 GMT
From: orwant@fahrenheit-451.media.mit.edu (Jon Orwant)
To: tloser@valdemar.microserve.com (Thomas A. Loser)
Subject: Re: re help needed
Message-Id: <ORWANT.97Oct7133009@fahrenheit-451.media.mit.edu>
tloser@valdemar.microserve.com (Thomas A. Loser) writes:
I have been trying for several days now to write what *should* be a
simple expression.
Given something like: $fld = ' some test data ';
I simply need to strip off any leading and trailing spaces IF THEY EXIST
and retrieve the rest of the data as a single scaler. I thought this would
be simple - HA!
$fld =~ s/^\s+//; # strip initial whitespace
$fld =~ s/\s+$//; # strip final whitespace
-Jon
------------------------------------
Jon Orwant http://tpj.com
Editor & Publisher, The Perl Journal
------------------------------
Date: 7 Oct 1997 16:46:13 GMT
From: gerben@cs.vu.nl (Gerben Vos)
Subject: Re: Sorting in a file
Message-Id: <61dp0l$k3t$1@star.cs.vu.nl>
Paul Denman writes:
>I have a large file to work with, which is a comma-seperated ASCII text
>file.
>Is it possible to sort this file by the first field without reading the
>entire
>content into an array?
Yes.
I assume the main problem is that the file is large. I can think of
two ways:
1) Sort parts of the file to temporary files, then merge these files.
This is the approach taken by the Unix sort program if it encounters
large files.
2) If the lines of the file are very large, but the number of lines
is relatively moderate: read the file once, and construct an array
containing the first field and the offset in bytes from the start of
the file where that line starts. Sort that array. Then, in a second
pass, iterate over the sorted array, and for every element, seek to
the appropriate location in the file, and copy the line that starts
there to your output.
BTW, if your files are expected to be large, it's not bad Perl form
to use an external sorting program (in fact, it may be *good* form).
I haven't had luck with the Microsoft sort program that comes with NT,
but you can get a copy of GNU sort for Win32 from
ftp://ftp.cygnus.com/pub/gnu-win32/latest/ or a nearby directory.
``gsort -n input >output'' is all you need in that case.
[Call the sorting program "gsort" to avoid a naming collision with
Microsoft's sort program.]
g e r b e n @ c s . v u . n l . . . . . . . . . . . . G e r b e n V o s <><
Join the Coalition Against Unsolicited Commercial Email! http://www.cauce.org/
Men... the second-most confused group in our culture... after teenagers.
-- from John Mark Ministries' seminar on manhood
------------------------------
Date: Tue, 7 Oct 1997 13:06:42 -0400
From: phenix@interpath.com (John Moreno)
Subject: Re: Sorting values such as 1.1, 1.1.1, 2.1 into order
Message-Id: <19971007130642416847@roxboro-188.interpath.net>
Tony McDonald <tony.mcdonald@ncl.ac.uk> wrote:
] I really hope someone can help, I'm slowly losing it here.
]
] I have some values in an array;
] #!/usr/bin/perl
]
] $files[0] = "10.html";
] $files[1] = "1.html";
] $files[2] = "1.1.html";
] $files[3] = "1.2.html";
] $files[4] = "5.html";
] $files[5] = "4.1.html";
] $files[6] = "4.html";
] $files[7] = "10.1.html";
] $files[8] = "10.1.1.html";
] $files[9] = "1.1.1.html";
] $files[10] = "2.html";
]
] @files = sort(@files);
]
] foreach $filename (@files)
] {
] print "filename - $filename\n";
] }
You need to do your own comparison on the sort.
Here a routine I just wrote.
# sort using explicit subroutine name
sub bynum {
@anumbers = ($a =~ /([0-9]+)/g);
$arest=$';
@bnumbers = ($b =~ /([0-9]+)/g);
$brest=$';
$aindexes=@anumbers;
$bindexes=@bnumbers;
$lastindex=(@anumbers)-1;
$answer=0;
if ($aindexes>$bindexes) {$lastindex=$#bnumbers;$answer=1}
if ($aindexes<$bindexes) {$lastindex=$#anumbers;$answer=-1}
$i=0;
do {
if ($anumbers[$i] !=$bnumbers[$i]) {
if ($anumbers[$i]>$bnumbers[$i]) { $answer=1; }
else { $answer=-1; }
$i=$lastindex;
}
$i++;
} while ($i<=$lastindex);
if ($aindexes==0 || $bindexes==0) {
$answer=0;
$arest=$a;
$brest=$b;
}
if ($answer==0) {
$answer= $arest cmp $brest;}
return $answer;
}
then instead of: @files = sort(@files);
use: @files = sort bynum @files;
This is by no means the most efficient way to write this, nor is it the
best commented or for that matter the best anything - but it'll work.
But it does what you want and will still has some allowances for the
occasional item to not fit the format - as far as I can tell it'll sort
correctly for every case except 1: when the string has numbers in it
that aren't leading. IOW 1.html and jack.html will both sort correctly
but jack1.html won't.
--
John Moreno
------------------------------
Date: Tue, 07 Oct 1997 11:45:34 -0700
From: Tom Bortels <bortels@pacificnet.net>
Subject: Strange string catenation quirk
Message-Id: <343A834E.40D4@pacificnet.net>
Weird. Try the following:
#!/usr/local/bin/perl
$hello = "hello"; $goodbye = "goodbye";
$r1 = "$hello:$goodbye:::::\n";
$r2 = "$hello:$goodbye" . ":::::\n";
print $r1;
print $r2;
(I was using similar code to munge thru a shadow password file)
Here's the output I get:
hello::
hello:goodbye:::::
I understand the second line (and that's how I managed to solve the
problem I was working on), but I don't understand what is causing the
first line to do what it is. Programming Perl says that :: is a
an operator that lets you get to the symbol table, but I still can't
seem to wrap my brain around this. Does anyone out there have a good
perl flashlight that can shine some light on this code? As I mentioned,
I solved it by segregating my bunch-o-colons, but now I'm curious...
-- Tom Bortels
bortels@pacificnet.-please-don't-spam-me-you-scurvied-usenet-robots.net
------------------------------
Date: Tue, 07 Oct 1997 10:21:21 -0700
From: Kin Cho <kin@isi.com>
Subject: system() won't run shell script correctly
Message-Id: <343A6F91.2921FD2D@isi.com>
This command executes correctly in a shell script:
xterm -e rlogin foo
The xterm comes up and lets me login to foo.
However, the same command in a Perl script does not:
system("xterm -e rlogin foo")
the xterm just flashes and goes away.
What gives?
Some kind of stty problem?
Please help if you know how this works.
-kin
------------------------------
Date: 7 Oct 1997 18:50:35 GMT
From: neilb@zetnet.co.uk (Neil Briscoe)
Subject: Re: system() won't run shell script correctly
Message-Id: <memo.19971007195031.14937A@skep.compulink.co.uk.cix.co.uk>
In article <343A6F91.2921FD2D@isi.com>, kin@isi.com (Kin Cho)
wrote:
> This command executes correctly in a shell script:
>
> xterm -e rlogin foo
>
> The xterm comes up and lets me login to foo.
> However, the same command in a Perl script does not:
>
> system("xterm -e rlogin foo")
>
> the xterm just flashes and goes away.
>
> What gives?
> Some kind of stty problem?
>
> Please help if you know how this works.
>
Well, when you run the command from the shell, it runs the
command, without exiting, because it fork()s a copy of itself
and then exec(s) the command you asked for.
The system command spawns a shell to run the command, and then
the shell terminates, removing the foundation for the xterm
process, which dies of fright.
If your perl script is just a one liner to do this, then you
might do :-
exec("xterm -e rlogin foo");
which will cause your perl script to transform into the command
you want.
If you need your perl script to remain unmolested (and do other
things) then you need to fork() another process, and then have
the child run the exec() command.
Regards
Neil
------------------------------
Date: Tue, 07 Oct 1997 14:33:17 -0400
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: Using MAKE from Perl 4 script
Message-Id: <-0710971433170001@aggie.coaps.fsu.edu>
In article <343A50F1.412A@panix.com>, phanly@seamans.com wrote:
+ I'm trying to run a makefile from a Perl 4 script in the following way:
+ $CMD = "make -f test.mk"
+ system("$CMD");
+ For some reason, the makefile seems to only run half way through and
+ then just stops. If I run the makefile from the command line, it runs to
+ completion without any problems.
I suspect you need to turn on auto-flushing. Try setting "$|=1;". Does
the 'make' actually do what it is supposed to, even tho you don't see
it finish?
BTW, perl4 is dead, dead as a doornail. You should consider upgrading
to v5 as soon as you can.
James
--
Consulting Minister for Consultants, DNRC
Support the anti-Spam amendment <url:http://www.cauce.org/>
To cure your perl CGI problems, please look at:
<url:http://www.perl.com/perl/faq/idiots-guide.html>
------------------------------
Date: Tue, 07 Oct 1997 14:15:54 -0400
From: Ryan Ilea <rilea@NospaMsprint.ca>
Subject: Where can I find LWP 5.02 or later?
Message-Id: <343A7C59.81@NospaMsprint.ca>
Hi,
I'm trying to compile a perl program that I have and first I had to
upgrade perl from 5.001 to 5.003 and now it says that it requires LWP
5.02 or later. If anyone knows where I can get LWP for Linux I would
really appreciate if they would let me know. Thanks in advance.
Ryan
PS If replying by email remove 'NospaM' from address.
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 1139
**************************************