[8415] in Perl-Users-Digest
Perl-Users Digest, Issue: 2032 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 5 12:28:58 1998
Date: Thu, 5 Mar 98 09:00:25 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 5 Mar 1998 Volume: 8 Number: 2032
Today's topics:
Re: @INC and lib <barnett@houston.Geco-Prakla.slb.com>
Re: A perl question <jdporter@min.net>
Re: Are perl $SIG{} handlers inherently unsafe? (Ken Fox)
CGI.pm and taint checking <johnc@interactive.ibm.com>
Re: Change Unix environment variable using Perl (Snow White)
Re: Change Unix environment variable using Perl <Tony.Curtis+usenet@vcpc.univie.ac.at>
Re: context-sensitive user defined functions? (Mike Stok)
Re: context-sensitive user defined functions? <ajohnson@gpu.srv.ualberta.ca>
Re: context-sensitive user defined functions? (Kevin B Cohen)
Re: Displaying subdirectories and not filenames <koos_pol@bigfoot.com>
Re: Displaying subdirectories and not filenames <koos_pol@bigfoot.com>
Re: Enable comp.lang.perl.misc by reading this message <barnett@houston.Geco-Prakla.slb.com>
Formatting telephone numbers - How to? <jjames@netguild.com>
HELP !! permutations ? <venky@clam.com>
Re: HELP !! permutations ? <ajohnson@gpu.srv.ualberta.ca>
in the name of Baby Jesus PLEASE HELP ME... <boatdrinks10@hotmail.com>
Re: isn't perl clever: short-circuit logic <jdporter@min.net>
Re: Multidimensional Hashes <jdporter@min.net>
Re: Need help on script <jdporter@min.net>
padding a file -- Better way?? <dcordero@giss.nasa.gov>
Perl (CGI)/database <GholamReza.Azimi@iu.hioslo.no>
Re: Q: How to <jdporter@min.net>
Re: Script for RTrimming Fields <ajohnson@gpu.srv.ualberta.ca>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 05 Mar 1998 09:33:08 -0600
From: Dave Barnett <barnett@houston.Geco-Prakla.slb.com>
Subject: Re: @INC and lib
Message-Id: <34FEC5B4.72EB6776@houston.Geco-Prakla.slb.com>
jabedi@its.brooklyn.cuny.edu wrote:
>
> Hi,
>
> I'm very new to perl and its way. I know from reading various posts that the
> following will add new perl library paths,
>
> use lib '/new/perl/lib/dir';
>
> However, I'm not very clear on where/when this is used.
Okay.
> Is 'use' a shell
> command?
Depending on how you invoke things, nothing in perl is a shell command.
Or everything is. ;-)
> Is the above command to be included in the Makefile?
You might be able to, but that is too restrictive, don't you think?
> or some other
> configuration file?
No need for a configuration file, really.
> at compile time?
I believe that is the case.
> Or perhaps, afterward? Can anyone please
> clear my questions?
I hope so.
See perldoc -f use
See Blue camel function section on use.
The use lib keyword combination is a great way to use private
libraries. Things that you write yourself, but don't want generally
used, things from CPAN that your ISP|Systems Admin|Someone else will not
install in the default libraries, ...
It is inserted into your perl programs. A small contrived example might
be:
#!/usr/local/bin/perl -w
#
use strict;
use diagnostics;
use lib '/my/perl/lib/dir'; # tells perl to put /my/perl/lib/dir in @INC
use myModule; # perl looks through @INC to find myModule
LOG("This is a log message."); # use LOG() subroutine from myModule
__END__
> Thanks!
>
HTH.
> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/ Now offering spam-free web-based newsreading
Dave
--
"Security through obscurity is no security at all."
-comp.lang.perl.misc newsgroup posting
------------------------------------------------------------------------
* Dave Barnett U.S.: barnett@houston.Geco-Prakla.slb.com *
* DAPD Software Support Eng U.K.: barnett@gatwick.Geco-Prakla.slb.com *
------------------------------------------------------------------------
------------------------------
Date: Thu, 05 Mar 1998 11:47:19 -0500
From: John Porter <jdporter@min.net>
Subject: Re: A perl question
Message-Id: <34FED717.4E57@min.net>
Sebastian Probst wrote:
>
> $variable=~s/" "/%20/ge
>
> should work.
What unspeakable mutant perl bastard are you working with?
The fact that you said "should work" indicates that you
really didn't know for a fact -- in which case a quick
visit to your perl interpreter would have been in order.
John Porter
------------------------------
Date: 5 Mar 1998 15:25:11 GMT
From: kfox@pt0204.pto.ford.com (Ken Fox)
Subject: Re: Are perl $SIG{} handlers inherently unsafe?
Message-Id: <6dmg4n$ejh1@eccws1.dearborn.ford.com>
Randal Schwartz <merlyn@stonehenge.com> writes:
> >>>>> "Ilya" == Ilya Zakharevich <ilya@math.ohio-state.edu> writes:
> >> Is it unsafe to even just do this in a server
> >> $SIG{CHLD} = sub { wait; };
> Ilya> You have seen the answer already. [It's unsafe]
> Of course, even if it *was* safe, it's dangerous code.
>
> Two SIGCHLDs could come in before it's your turn for CPU ...
> Dangerous Code. Don't do this.
Ugh. I thought SIGCHLD delivery was queued **. In fact, I believed
it so much that I had to write a test program just to prove Randal
wrong. ;) I loose.
So, if $SIG{CHLD} = sub { wait } doesn't work, why does the FAQ
recommend it? The only reaper that actually works right is the
"elaborate" one given in the signal pod. I think that one is still
broken on systems that don't immediately raise a SIGCHLD when a SIGCHLD
handler is registered if a terminated child already exists. (A child
can terminate between the wait and the re-registration of the handler.)
I'd actually like Perl to generate synthetic signals for SIGCHLD.
Maybe even for all signals. That would let Perl define intuitive
and portable semantics which would make user code a lot cleaner.
- Ken
** On a *real* operating system they would be. ;) I only ran my test
on Solaris. Do other Unix variants queue SIGCHLD? Does NT do
this right?
--
Ken Fox (kfox@ford.com) | My opinions or statements do
| not represent those of, nor are
Ford Motor Company, Powertrain | endorsed by, Ford Motor Company.
Analytical Powertrain Methods Department |
Software Development Section | "Is this some sort of trick
| question or what?" -- Calvin
------------------------------
Date: Thu, 05 Mar 1998 11:53:35 +0000
From: Huckle <johnc@interactive.ibm.com>
Subject: CGI.pm and taint checking
Message-Id: <34FE9232.E220EB21@interactive.ibm.com>
I am using CGI.pm for the first time in a major project. I am have taint
checking turned on (-T). The problem is that I am not being forced to
untaint my variables. I am using 3 user inputs in an open command and it
works just fine.
Is CGI.pm causing these variables to be untainted? If so, why? That
seems stupid. Or am I doing something wrong? Is there another way to
turn on taint checking that is better than the -T command?
--
John Call
------------------------------
Date: Thu, 05 Mar 1998 16:06:05 GMT
From: anybody2@bellsouth.net (Snow White)
Subject: Re: Change Unix environment variable using Perl
Message-Id: <889112182.29@209.138.27.204>
In article <x6eogzmm2vz.fsf@pearl.fi.bear.com> on 04 Mar 1998 18:50:56 -0500,
Justin Vallon <vallon@pearl.fi.bear.com> wrote:
> Environment variables are only passed down to children. They are
> never passed up to parents.
> That is, system runs your command in a shell, which changes the
> value of MY_EV, then exits. perl's variables are not affected.
> If you use $ENV{"SHELL"}, you have changed the value of perl's
> environment variable SHELL. Processes that you start, via exec
> or system, will inherit this value. Your parent is not
> affected. When you (the perl script) exits, the process and its
> environment variables disappear.
Will this work if I run a process using backquotes?
It does not seem to with Perl5.003 under Solaris.
I have developed some code running Perl5.004 under OS/2
which includes statements like:
...
$ENV{'SOMETHING'} = "somevalue";
...
$Result = `perl -x $Program`;
...
but the script represented by $Program does not see the new value
of SOMETHING when running under Solaris (SunOS 5.5) though it does
under OS/2. I mentioned this to a colleague who replied that she
thought it was a problem with the way Solaris handles its
environment. Is she right? Anyone know what is wrong here?
------------------------------
Date: 05 Mar 1998 17:24:09 +0100
From: Tony Curtis <Tony.Curtis+usenet@vcpc.univie.ac.at>
Subject: Re: Change Unix environment variable using Perl
Message-Id: <7x7m69kswm.fsf@beavis.vcpc.univie.ac.at>
Re: Change Unix environment variable using Perl, Snow
<anybody2@bellsouth.net> said:
Snow> Will this work if I run a process using backquotes?
Yes, it's still a child process.
Snow> It does not seem to with Perl5.003 under Solaris.
The current version is 5.004, upgrade!
Snow> $ENV{'SOMETHING'} = "somevalue";
Snow> $Result = `perl -x $Program`;
Snow> ..
works fine for me with perl5.004_04 (and perl5.003)
$ENV{FOOBAR} = "banana";
$env = `printenv`;
print $env;
==> FOOBAR=banana
so the child process running printenv inherited FOOBAR.
So, I think either your use of the -x switch isn't doing
what you think it is, or your perl installation is broken.
hth
tony
--
Tony Curtis, Systems Manager, VCPC, | Tel +43 1 310 93 96 - 12; Fax - 13
Liechtensteinstrasse 22, A-1090 Wien, AT | http://www.vcpc.univie.ac.at/
"Everything I am, I learned on the back of cereal boxes" ~ RJ, "Over the Hedge"
------------------------------
Date: 5 Mar 1998 15:59:41 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: context-sensitive user defined functions?
Message-Id: <6dmi5d$9en@news-central.tiac.net>
In article <34FEC559.C72C3A1E@fmr.com>,
David Boyce <David.Boyce@fmr.com> wrote:
>You know how lots of perl built-in functions are context sensitive, i.e.
>they return one thing in a scalar context and another in a list
>context? I'm wondering if/how it's possible to write your own functions
>which do the same thing. This feels like a FAQ but does not appear to
>be one. To elaborate, imagine a function which generates textual
>output; in some cases the user wants the output dumped to stdout/stderr
>and only cares to check the return status:
>
> $status = command(...);
>
>and in other cases he wants to capture that output and do some
>post-processing on it:
>
> %results = command(...);
You should have a look at wantarray which allows a function to find out
what kind of context it was called in. It's documented in the perlfunc
documentation.
Note that you can't tell what happens to the value you return (e.g. if a
list is wanted you can't tell if it's going to be assigned to an array, a
hash or have something completely different done to it.)
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
stok@colltech.com | Collective Technologies (work)
------------------------------
Date: Thu, 05 Mar 1998 10:11:12 -0600
From: Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
Subject: Re: context-sensitive user defined functions?
Message-Id: <34FECEA0.5625B18A@gpu.srv.ualberta.ca>
David Boyce wrote:
>
> You know how lots of perl built-in functions are context sensitive, i.e.
> they return one thing in a scalar context and another in a list
> context? I'm wondering if/how it's possible to write your own functions
> which do the same thing. This feels like a FAQ but does not appear to
you want to check out the wantarray() function
perldoc -f wantarray
regards
andrew
------------------------------
Date: 5 Mar 1998 11:23:10 -0500
From: kcohen@julius.ling.ohio-state.edu (Kevin B Cohen)
Subject: Re: context-sensitive user defined functions?
Message-Id: <6dmjhe$83e@julius.ling.ohio-state.edu>
In article <34FEC559.C72C3A1E@fmr.com>,
David Boyce <David.Boyce@fmr.com> wrote:
>be one. To elaborate, imagine a function which generates textual
>output; in some cases the user wants the output dumped to stdout/stderr
>and only cares to check the return status:
<snip>
>and in other cases he wants to capture that output and do some
>post-processing on it:
when i've wanted something similar to this, i've passed a "flag" to
the function, along with the input data. then, after munging the
input, i evaluate the flag:
if ($my_flag == 1) {
push (@output_array, $munged_data);
return;
}
elsif ($my_flag == 2) {
return ($munged_data);
}
kevin
------------------------------
Date: Thu, 05 Mar 1998 16:57:14 +0100
From: Koos Pol <koos_pol@bigfoot.com>
Subject: Re: Displaying subdirectories and not filenames
Message-Id: <34FECB59.F7454FCC@bigfoot.com>
>What you're calling a newsreader, "Mozilla 4.02 [en] (X11; I; SunOS
>5.5.1 sun4m)", only qualifies in a sick and perverse sense.
>That kind of code posting verges on the useless, and is
>deeply annoying. Please get a Real Newsreader (tm).
Thank you kindly Tom, but your response is also deeply annoying.
Communciator is what I have been given by our admins.
Nothing much I can do about it. I may set line wrapping at 200,
but who garantuees it is coming to you this way?
Besides, why is so costly about pressing DEL 20 times at the end of
the line for joining with the next?
You don't want if completely for free, do you? :-)
BTW, Do I need to forward your "sick and perverse" remark to our
sysadmins, or can I leave it with this?
I do like critics for improving my work, but this kind is kinda painful.
Koos Pol
--------------------------------------------------------------------------
S.C. Pol tel: +31 20 3116122
PC Systems Administrator email: Koos_Pol@nl.compuware.com
Compuware Europe PGP public key available upon request
A little inaccuracy sometimes saves tons of explanation.
-- H. H. Munroe
------------------------------
Date: Thu, 05 Mar 1998 17:07:45 +0100
From: Koos Pol <koos_pol@bigfoot.com>
Subject: Re: Displaying subdirectories and not filenames
Message-Id: <34FECDD1.563877AC@bigfoot.com>
Specially reposted for Tom Christiansen, with linewrap set at 200.
Hope you recieve it as I sent it, Tom.
Have fun!
#!/usr/local/bin/perl
##################### Copyright #############################################
# #
# This program is Copyright 1998 by Koos Pol. #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# If you do not have a copy of the GNU General Public License write to #
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, #
# MA 02139, USA. #
##############################################################################
##############################################################################
# #
# Display a tree structure like the DOS tree command #
# #
# Usage: perl tree.pl [source [depth]] #
# #
##############################################################################
my $tree;
my $prefix;
my $maxlevel;
$tree = $ARGV[0];
$maxlevel = $ARGV[1];
if (! defined $tree) {$tree = '.'}
if ($tree =~ /\?/) {die ("Usage: perl tree.pl [source [depth]]\n")}
if ($maxlevel !~ /d*/) {die ("Usage: perl tree.pl [source [depth]]\n")}
if ($maxlevel == 0) {$maxlevel = -1}
$prefix = '';
disptree ($tree,$prefix,$maxlevel);
sub disptree {
my $tree;
my $maxlevel;
my $current_entry;
my $next_entry;
my $prefix ;
my $more_dirs;
my $dirh;
my $skip_entry;
$tree = $_[0];
$prefix = $_[1];
$maxlevel = $_[2];
if ($maxlevel == 0) {return}
$tree =~ s|/*$|/|; # keep exactly one /
# Read the target directory
$dirh = 'DIRH'.$maxlevel; # create a unique dir handle
(opendir ($dirh , $tree)) || (warn "Can't read $tree $!\n");
# skip files until directory found
$skip_entry = 1;
while ($skip_entry) {
$current_entry = readdir($dirh);
if (! defined ($current_entry)) {$skip_entry = 0} # End Of Dir
elsif ( ($current_entry ne '.') && ($current_entry ne '..') && (-d $tree.$current_entry)) {$skip_entry = 0;}
}
#
# --- Now check the rest of the entries for directories
#
if (defined $current_entry) { # did we hit EOD (EndOfDir) ?
$more_dirs = 1; # No, raise the 'more_dirs' flag
while ($more_dirs) {
# skip files until directory found
$skip_entry = 1;
while ($skip_entry) {
$next_entry = readdir ($dirh);
if (-d $tree.$next_entry) { $skip_entry = 0 } ;
}
# process the current dir
print ($prefix.'+ '.$current_entry."\n"); # print the current directory
if (defined $next_entry) { # is this the last dir ?
disptree ($tree.$current_entry , $prefix.'| ' , $maxlevel-1); # no, prepend the 'history'
$current_entry = $next_entry;
} else {
disptree ($tree.$current_entry , $prefix.' ' , $maxlevel-1); # yes. print this entry plainly
$more_dirs = 0;
}
}
}
closedir $dirh;
}
--
Koos Pol
----------------------------------------------------------------------
S.C. Pol tel: +31 20 3116122
PC Systems Administrator email: Koos_Pol@bigfoot.com
Compuware Europe PGP public key available upon request
A little inaccuracy sometimes saves tons of explanation.
-- H. H. Munroe
------------------------------
Date: Thu, 05 Mar 1998 09:47:20 -0600
From: Dave Barnett <barnett@houston.Geco-Prakla.slb.com>
Subject: Re: Enable comp.lang.perl.misc by reading this message
Message-Id: <34FEC908.C0259F44@houston.Geco-Prakla.slb.com>
rabin@criterion.canon.co.uk wrote:
>
> This group seems to be unread
Sometimes unanswered, but not unread. Have a look at DejaNews to see
how unpopular this group is.
> and has been taken off our news feed.
That kinda sucks.
> Reading
> this message may re-activate the feed.
Maybe responding will help further?
> If it does not, you
who me???
> may be required to
> make a business case to have the group added to the automated list of groups.
>
Holy non-sequiter, Batman! :-)
It's already on my newsservers' list of available groups.
Maybe you need to do something else?
> Rabin
Good luck.
Dave
--
"Security through obscurity is no security at all."
-comp.lang.perl.misc newsgroup posting
------------------------------------------------------------------------
* Dave Barnett U.S.: barnett@houston.Geco-Prakla.slb.com *
* DAPD Software Support Eng U.K.: barnett@gatwick.Geco-Prakla.slb.com *
------------------------------------------------------------------------
------------------------------
Date: 5 Mar 1998 16:04:34 GMT
From: "Joanne James" <jjames@netguild.com>
Subject: Formatting telephone numbers - How to?
Message-Id: <01bd484d$f5534a40$12bbcdcf@AGDEV001.AGDEV>
Keywords: format, formatting
Hi. I'm helping out on a project using Perl and am learning as I go. I've
been trying to show telephone numbers (numbers only are stored on database
-no
dashes) on an html page with dashes inserted. The database contains both
US and international phone numbers. I want to put in dashes as in the
following:
999-999-9999 in all cases. I just need an example. Assume you are getting
a field from an Oracle database and want to put it into an array (I
presume), then insert the dashes. How would you do it? I have been trying
for hours to get this to work! I would appreciate some feedback. Thanks
for the
help!
------------------------------
Date: Thu, 05 Mar 1998 10:54:53 -0500
From: Venkatesh <venky@clam.com>
Subject: HELP !! permutations ?
Message-Id: <34FECACD.6E40@clam.com>
hi,
I am looking for a simple way to generate some permutations
on an array.
I have an array with integers/strings
@int_array = ( '1', '2', '3', '4', '5', '6' ) ;
I am looking to generate lists like
(1,1) (1,2) (1,3) (1,4 ) ...
(2,1) (2,2) (2,3) (2,4 ) ...
(1,2,3) ( 1,2,4 ) (1,2,5) (1,2,6)
(2,3,4) (2,3,5) (2,3,6 ) ...
(1,2,3,4 ) , (1,2,3,5) etc.
The application that I have needs to use the permutations to
check for certain conditions. ( 1,2 ) and (2,1) are the same
as far the application is concerned.
Is there an easy way of doing this in perl ? The tough way is
to iterate & iterate ....
Venkatesh V
Clam Associates
------------------------------
Date: Thu, 05 Mar 1998 10:22:14 -0600
From: Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
Subject: Re: HELP !! permutations ?
Message-Id: <34FED136.700BED48@gpu.srv.ualberta.ca>
Venkatesh wrote:
!
!
! hi,
!
! I am looking for a simple way to generate some permutations
! on an array.
[snip]
! The application that I have needs to use the permutations to
! check for certain conditions. ( 1,2 ) and (2,1) are the same
! as far the application is concerned.
that would be combinations then...
! Is there an easy way of doing this in perl ? The tough way is
! to iterate & iterate ....
a quick faqgrep on 'permute' shows:
[danger:ajohnson:~]$ faqgrep permute
perlfaq4.pod:=head2 How do I permute N elements of a list?
the entry therein contains a recursive permut() function
written by Tom Christiansen.
hope it helps
regards
andrew
------------------------------
Date: Thu, 05 Mar 1998 11:05:28 -0800
From: Swamp Midget <boatdrinks10@hotmail.com>
Subject: in the name of Baby Jesus PLEASE HELP ME...
Message-Id: <34FEF778.615A@hotmail.com>
Would someone please tell me how I can call a C executable from a PERL
script. I know I'm an idiot. Please help.
thanx,
Swamp Midget_
------------------------------
Date: Thu, 05 Mar 1998 11:31:02 -0500
From: John Porter <jdporter@min.net>
Subject: Re: isn't perl clever: short-circuit logic
Message-Id: <34FED346.CA3@min.net>
kevin@cmhcsys.com wrote:
>
> today i found myself writing two short-circuit lines in a row, and it got me
> to wondering if i was really being as efficient as i could, or not. here's
> the code, edited somewhat for clarity:
>
> $InitialConsonants = "bcdfghjklmnpqrstvwxyz";
>
> # this first short-circuit line sets $onset if there be one,
> # and puts the syllable into $rime if there be'n't one.
>
> (($onset) = /^([$Conset]+)/) || ($rime = $syllable);
I think your "editing for clarity" has gone over to the dark side. :-)
That pattern match is implicitly matching $_, so I assume you set
$syllable = $_ somewhere above that point.
I also assume $Conset = $InitialConsonants, right?
I guess I'd do it this way:
my( $onset, $rime ) = /^([$Conset]*)(.*)$/;
Not that I wish to dampen your enthusiasm for short-circuiting.
John Porter
------------------------------
Date: Thu, 05 Mar 1998 11:16:39 -0500
From: John Porter <jdporter@min.net>
Subject: Re: Multidimensional Hashes
Message-Id: <34FECFE7.38C9@min.net>
Earl Hood wrote:
>
> > $my_hash{$_} = '';
> > $my_hash{$_}{'d'} = $random_order{$_};
> --------------^^^^^
>
> Here's the gotcha. Reason: Perl tries to treat $my_hash{$_} as a hash
> reference that needs to be dereferenced. However, you have set the
> value to the empty string. Hence, Perl is treating the empty string
> value as the hash reference. Since you explicitly defined the keys to
> %my_hash, Perl will not automatically create the anonymous hash.
> This is only done if the $my_hash{$_} is not defined.
Actually perl doesn't mind. It does what you tell it to do.
The only time perl stops you from doing this is when
use strict 'refs' is in effect, which it wasn't in
Phivu's program.
John Porter
------------------------------
Date: Thu, 05 Mar 1998 11:08:11 -0500
From: John Porter <jdporter@min.net>
Subject: Re: Need help on script
Message-Id: <34FECDEB.185@min.net>
nathan@cyberservices.com wrote:
>
> open(USERS, ">$datafile")
> print USERS "$newdatafiletxt";
Have you tried running this from the command line (shell prompt)?
This looks like a syntax error, which means when you try to run
it, it never gets as far as the first executable statement.
More importantly, why aren't you using CGI.pm?
You would save yourself many many headaches if you would use the
CGI module. And then use CGI::Carp too.
hth,
John Porter
------------------------------
Date: Thu, 05 Mar 1998 10:57:15 -0500
From: Douglas Cordero <dcordero@giss.nasa.gov>
Subject: padding a file -- Better way??
Message-Id: <34FECB5B.41C6@giss.nasa.gov>
Hi all:
I wrote a subroutine to space pad a variable length
text array to 80 left justified characters.
Does it look OK? Is there a better way?
Is there a module or something that has this built-in?
Thx.
sub padder {
my( @val ) = @_;
my( @paddedval );
@paddedval = ();
foreach (@val) {
@paddedval = (@paddedval, sprintf "%-80.80s", $_ );
}
return @paddedval;
}
------------------------------
Date: 5 Mar 1998 16:31:52 GMT
From: "Gholam Reza Azimi" <GholamReza.Azimi@iu.hioslo.no>
Subject: Perl (CGI)/database
Message-Id: <01bd4854$1173dae0$5c4a2780@0060979f885b.iu.hioslo.no>
Hello everyone
I've developed a database in Sybase. What I would like to know is how to
process this file through my Perl-scripts for processing and data storage!
SQL is one way to query data from this file. But what about storage?
can anyone help me with that!
Reza
------------------------------
Date: Thu, 05 Mar 1998 10:55:16 -0500
From: John Porter <jdporter@min.net>
Subject: Re: Q: How to
Message-Id: <34FECAE4.6305@min.net>
Lance wrote:
>
> This data file is like this, they are all small files:
> .1 14.650 .2155123 .0
>[...]
> .1 15.00001 .2017127 .0
>
> As you can see, the length is varying. I have no idea what the record size's
> going to be. So how to get the last record is still a problem. What I do now
> is:
> open(FF,$input) || die "Couldn't open $input: $!\n";
> $i=0;
> while (<FF>) {
> ($a1[$i],$a2[$i],$a3[$i],$a4[$i])= split(" ",$_);
> $$i++;
> }
> print "third field of last record is $a3[$i]\n";
> But this seems awkward and it generates three useless
> variables:a1[],a2[],a4[].
#( you've got a typo: that should be $i++, not $$i++. )
By the way, split, when given no arguments, is exactly
equivalent to what you have, so you could have said:
($a1[$i],$a2[$i],$a3[$i],$a4[$i]) = split;
If the ONLY field you're ever interested in is the third
one on the last line, then this would do:
while (<FF>) {
($a,$b,$c,$d) = split;
}
print "third field of last record is $c\n";
because $c gets re-set by each iteration through the loop.
You may want a more general solution for parsing those
data files. Some day you may want the third field from
ALL the records; or all the fields from each record whose
first field is not .1. Or whatever.
Since the data file is a like a table, or two-dimensional
array, it is natural to use perl's built-in capability for
two-dimensional arrays. That means you can have it so
that $a[0][0] is the first record, first field;
$a[9][1] is the tenth record, second field; and so on.
So let's rework that code a little bit:
my $i = 0;
for (<FF>) {
my @t = split;
$a[$i] = \@t; # huh?
$i++;
}
print "last record, third field is $a[$i][2]\n";
The backslash is needed because you have to store a
reference to the array @t, rather than @t itself.
That's because arrays can only contain scalar values --
numbers, strings, and references.
Another way to get a reference to a list is to put the
list in square brackets:
for (<FF>) {
my @t = split;
$a[$i] = [ @t ]; # a ref to a copy of @t
$i++;
}
This is slightly different, in that it gives a reference to
a copy of @t, rather than a ref to @t itself as before.
Now we can dispense with the temporary variable:
for (<FF>) {
$a[$i] = [ split ];
$i++;
}
Since you're going sequentially up through the @a array,
you could just push each new value onto it -- assuming
that @a starts out empty. (If it isn't, you can make it
empty with @a=();) That way you don't need the counter
variable $i:
for (<FF>) {
push( @a, [ split ] );
}
Now, if you think about it, the entries in @a correspond
directly to the lines in the data file. That is, there
is a one-to-one mapping from one to the other. This
sounds like a good candidate for perl's "map" operator!
my @a = map { [ split ] } <FF>;
print "last record, third field = $a[-1][2]\n";
Whoa!
hth,
John Porter
------------------------------
Date: Thu, 05 Mar 1998 10:08:26 -0600
From: Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
Subject: Re: Script for RTrimming Fields
Message-Id: <34FECDFA.1DADCFCA@gpu.srv.ualberta.ca>
Byron Lee wrote:
>
> Hello,
>
> Does anyone out there have a script for trimming spaces from
> the end of a string? I suppose I could write one, but I'm
> taking the lazy approach today. :)
for better luck receiving help, use a better
form of laziness: check the faqs first.
perldoc perlfaq4
andrew
------------------------------
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 2032
**************************************