[12106] in Perl-Users-Digest
Perl-Users Digest, Issue: 5706 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 17 22:15:25 1999
Date: Mon, 17 May 99 19:00:18 -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 Mon, 17 May 1999 Volume: 8 Number: 5706
Today's topics:
Re: A Perl Script <collin.starkweather@colorado.edu>
Bandwidth [Was: Re: man pages and FAQs: why posted? lvirden@cas.org
Re: DBI not thread safe. Another way? <sugalskd@netserve.ous.edu>
Re: FAQ 4.15: How do I find yesterday's date? <bill@fccj.org>
Re: FAQ 4.15: How do I find yesterday's date? <rra@stanford.edu>
Re: FAQ 4.15: How do I find yesterday's date? <rra@stanford.edu>
Re: FAQ 4.15: How do I find yesterday's date? (Lee)
Re: help needed <tex2121@my-dejanews.com>
Re: how to read in line by line? <cassell@mail.cor.epa.gov>
Re: Monadic classes, eponymous metaobjects, and translu (Stefaan A Eeckels)
Re: multiply dimesioned arrays, passed by reference <rra@stanford.edu>
Re: negative subscripts <rra@stanford.edu>
Re: Parsing a data file... (Larry Rosler)
Re: Perl "constructors" <rra@stanford.edu>
Re: Perl to C translator? <rra@stanford.edu>
Re: Sorting problem (Arjun Ray)
Re: Using split with map, then join <pvorishatesspam@earthlink.net>
Re: Using split with map, then join (Andrew Allen)
Re: Using split with map, then join (Larry Rosler)
Re: Using split with map, then join (Larry Rosler)
Re: Using split with map, then join (Larry Rosler)
Your assistance humbly requested (file processing probl <josh@bitwell.net>
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 17 May 1999 18:57:49 -0600
From: Collin Starkweather <collin.starkweather@colorado.edu>
To: Lone Wolf <lone.wolf@net.ntl.com>
Subject: Re: A Perl Script
Message-Id: <3740BB0D.A17BF64A@colorado.edu>
> It has to be a configuration problem with the server can you suggest
> anything as I have had enough and am going to rebuild the server tomorow.
That would be a problem since I'm running NT and MIIS and it works on my
machine. And this is a virgin machine which still has the default
/scripts settings. Nothing fancy.
Knowing nothing else, I would use the Microsoft Solution for What Ails
You: Uninstall MIIS and Perl. Reinstall MIIS. Now reinstall Perl.
At least with the scripts I forwarded, you know you've got code that
works on an NT 4.0 build 1381 SP4 box with MIIS 3.0 running ActiveState
Perl 5.00502 with CGI module 2.42.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Collin Starkweather (303) 492-4784
University of Colorado collin.starkweather@colorado.edu
Department of Economics http://ucsu.colorado.edu/~olsonco
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
------------------------------
Date: 18 May 1999 01:41:53 GMT
From: lvirden@cas.org
Subject: Bandwidth [Was: Re: man pages and FAQs: why posted?
Message-Id: <7hqgh1$bkh$1@srv38s4u.cas.org>
:In article <B360D47A9668ADDBC8@0.0.0.0>,
: rlb@intrinsix.ca (Lee) writes:
:: same reasons. But I don't think it will be effective, and I think it
:: is an enormous waste of bandwidth. Hence sentence two of my post. The
Think of it this way - if the bandwidth wasn't being used on the Perl FAQs,
it would be used for porn pictures in the alt.binaries.* groups, flames
in one of the politics groups, or more newbie postings asking the FAQs
in this group... So if the bandwidth is going to be used anyways, it might
as well be used on something like the FAQs.
--
<URL: mailto:lvirden@cas.org> Quote: Saving the world before bedtime.
<*> O- <URL: http://www.purl.org/NET/lvirden/>
Unless explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
------------------------------
Date: 18 May 1999 00:51:06 GMT
From: Dan Sugalski <sugalskd@netserve.ous.edu>
Subject: Re: DBI not thread safe. Another way?
Message-Id: <7hqdhq$3kh$1@news.NERO.NET>
Darrel Davis <darreld@his.com> wrote:
: I have been blissfully building a multi-threaded server that connects
: to a database in thread via DBI , messes with data and builds return
: strings and sends tem back to the client. I read last night that DBI is
: not thread safe and is recommended to NOT open DBI with > 1 thread.
Perl itself isn't thread-safe unless you're very careful. (Which is to
say, explicitly lock *any* variable that might be accessed by multiple
threads) However...
What you want here is a thread dedicated to DBI, and funnel all your
database requests through it. You can do this via a queue--have the DBI
thread sitting around waiting on the queue,and have your other threads
send their requests into the queue.
Dan
------------------------------
Date: Mon, 17 May 1999 20:32:54 -0400
From: "Bill Jones" <bill@fccj.org>
Subject: Re: FAQ 4.15: How do I find yesterday's date?
Message-Id: <3740b51f.0@usenet.fccj.cc.fl.us>
In article <7hq60v$s59$1@news.NERO.NET>, stanley@skyking.OCE.ORST.EDU (John
Stanley) wrote:
> In article <373b9118@cs.colorado.edu>,
>> Tom Christiansen <perlfaq-suggestions@perl.com> wrote:
>> $yesterday = time() - ( 24 * 60 * 60 );
>
> Except that 24*60*60 isn't always "one day", and thus can give the wrong
> answer. Refer to DejaNews for a more complete discussion of this
> problem, which takes place in this group on a regular basis.
>
I am too lazy to look at DejaNews again...
Pray tell, when isn't there 60 seconds in
each of the 60 minutes which, as a group,
each make up 24 hours in a 'normal day'?
Trying to pull a "There is really only 23:59:58 in
a day doesn't count..."
Or, if you prefer, we could go off on the
There is really only 360 business days in a
year - those others are thrown out..."
-Sneex- :]
______________________________________________________________________
Bill Jones Data Security Specialist http://www.fccj.org/cgi/mail?dss
______________________________________________________________________
We are the CLPM... Lower your standards and surrender your code...
We will add your biological and technological distinctiveness to
our own... Your thoughts will adapt to service us...
...Resistance is futile...
Jacksonville Perl Mongers
http://jacksonville.pm.org
jax@jacksonville.pm.org
------------------------------
Date: 17 May 1999 18:29:10 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: FAQ 4.15: How do I find yesterday's date?
Message-Id: <ylpv3ztbbt.fsf@windlord.stanford.edu>
Bill Jones <bill@fccj.org> writes:
> Pray tell, when isn't there 60 seconds in each of the 60 minutes which,
> as a group, each make up 24 hours in a 'normal day'?
Days do not always have 24 hours in a time zone that has daylight saving's
time.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 17 May 1999 18:21:41 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: FAQ 4.15: How do I find yesterday's date?
Message-Id: <yl1zgfuq8q.fsf@windlord.stanford.edu>
Chris Nandor <pudge@pobox.com> writes:
> stanley@skyking.OCE.ORST.EDU (John Stanley) wrote:
>> Tom Christiansen <perlfaq-suggestions@perl.com> wrote:
>>> How do I find yesterday's date?
>>> The `time()' function returns the current time in seconds since the
>>> epoch. Take one day off that:
>>> $yesterday = time() - ( 24 * 60 * 60 );
>> Except that 24*60*60 isn't always "one day", and thus can give the
>> wrong answer. Refer to DejaNews for a more complete discussion of this
>> problem, which takes place in this group on a regular basis.
> But time() - (24*60*60) will always give you the previous day.
No, it won't. A day can be 25 hours, so if you have a start time within
an hour of midnight, you can get the wrong answer.
Tom, feel free to include the code available at:
http://moiraine.dimensional.com/~dgris/cgi-bin/pfr?func=yesterday&type=exact
in the FAQ for the more precise answer.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: Mon, 17 May 1999 20:56:54 -0500
From: rlb@intrinsix.ca (Lee)
Subject: Re: FAQ 4.15: How do I find yesterday's date?
Message-Id: <B366331696689BFC4@204.112.166.88>
In article <pudge-1705991858090001@192.168.0.77>,
pudge@pobox.com (Chris Nandor) wrote:
>In article <7hq60v$s59$1@news.NERO.NET>, stanley@skyking.OCE.ORST.EDU
>(John Stanley) wrote:
>
># In article <373b9118@cs.colorado.edu>,
># Tom Christiansen <perlfaq-suggestions@perl.com> wrote:
># >(This excerpt from perlfaq4 - Data Manipulation
># > ($Revision: 1.46 $, $Date: 1999/04/20 18:59:53 $)
># >part of the standard set of documentation included with every
># >valid Perl distribution, like the one on your system.
># >See also http://language.perl.com/newdocs/pod/perlfaq4.html
># >if your negligent system adminstrator has been remiss in his duties.)
># >
># > How do I find yesterday's date?
># >
># > The `time()' function returns the current time in seconds since the
># > epoch. Take one day off that:
># >
># > $yesterday = time() - ( 24 * 60 * 60 );
>#
># Except that 24*60*60 isn't always "one day", and thus can give the wrong
># answer. Refer to DejaNews for a more complete discussion of this
># problem, which takes place in this group on a regular basis.
>
>But time() - (24*60*60) will always give you the previous day. Since we
>are looking for a date, not a specific time, it is sufficient.
Except for those (2*60*60) seconds per year when it does not.
Lee
------------------------------
Date: Tue, 18 May 1999 01:05:21 GMT
From: tex2121 <tex2121@my-dejanews.com>
Subject: Re: help needed
Message-Id: <7hqech$bf7$1@nnrp1.deja.com>
Thanks anyways, I figured it out on my own.
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: Mon, 17 May 1999 17:30:55 -0700
From: David Cassell <cassell@mail.cor.epa.gov>
To: Yang Chao <yangc@ra.comm.mot.com>
Subject: Re: how to read in line by line?
Message-Id: <3740B4BF.111D9E56@mail.cor.epa.gov>
[courtesy cc to poster]
Yang Chao wrote:
>
> Hi,
>
> I am trying to write a piece of perl script to process a file.
> I want to open a file and then read from it line by line.
> I am trying to use the following perl script, but it seems
> it only read in one line out of every two lines. Please give
> me some ideas or suggestions, Thanks!
I managed to extract some of your code out of that gorp you
attached. I immediately noticed this:
> while (<IN>){
>
> $line = <IN>;
<> is the line-reading operator. Every time you invoke it,
it reads one (or more, depending on context) lines. So the
first line above reads in the first line of IN, and assigns
it to $_ .. which you throw away. Then you read the next
line, and assign that to $line. _ad_infinitum_. You only
get the even records into $line like this. Take out the
second line above, and use the $_ variable, which is
simpler.
> --------------------------------------------------------------------------------
>
> Name: try.pl
> try.pl Type: Perl Program (application/x-perl)
> Encoding: 7bit
This is the sort of stuff that shows up when you attach
stuff. It's not easily readable, and looks ghastly for
people who have standard newsreaders. Please, in future
remember that UseNet is a text-only medium, and do not
post MIME attachments. Just include the [appropriately
trimmed if needed] program into your post.
Thank you,
David
--
David Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: 17 May 1999 22:47:40 GMT
From: Stefaan.Eeckels@ecc.lu (Stefaan A Eeckels)
Subject: Re: Monadic classes, eponymous metaobjects, and translucent data members
Message-Id: <7hq6ac$1n7$2@justus.ecc.lu>
In article <sig14vfxxt.fsf@cre.canon.co.uk>,
Gareth Rees <garethr@cre.canon.co.uk> writes:
>
> Just be aware that there are people who will be confused!
That will always be the case. Those who *want* to understand
will not be confused.
--
Stefaan
--
PGP key available from PGP key servers (http://www.pgp.net/pgpnet/)
____________________________________________________________________
The great Confucius says: Mind like parachute, only works when open.
------------------------------
Date: 17 May 1999 18:11:59 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: multiply dimesioned arrays, passed by reference
Message-Id: <yl90anuqow.fsf@windlord.stanford.edu>
Gavin Sherlock <sherlock@genome.stanford.edu> writes:
> I'm trying to use a 3-dimensional array, which has been passed into a
> subroutine by reference. I am having huge troubles! If an array in the
> third dimension has no members, it can't be pushed onto, so for the
> first member, I have to use assignment.
Welcome to the wacky world of autovivification. The perllol man page is
what you want to read; it explains a lot of this stuff.
> @$familyArrayRef[$$numFamilies]=[$UID]; # have to assign first thing
> # to array
> push (@{@$familyArrayRef[$$numFamilies]}, $UID2); # now can push
> # onto it
Note that this is equivalent to:
$familyArrayRef->[$$numFamilies] = [ $UID, $UID2 ];
although it may not be that simple in your actual code.
> for ($extraDimension=0; $extraDimension<5; $extraDimension++){
> @$familyArrayRef[$$numFamilies]->[$extraDimension]=[$UID];
> push (@{@$familyArrayRef[$$numFamilies]->[$extraDimension}, $UID2);
Try this instead:
$familyArrayRef->[$$numFamilies][$extraDimension] = [ $UID ];
for the assignment. That should autovivify all of the intervening arrays
for you, without producing the warnings. Observe:
windlord:~> perl -w
use strict;
use Data::Dumper;
my $array = [ ];
$array->[0][3] = [ 5 ];
print Dumper ($array);
$VAR1 = [
[
undef,
undef,
undef,
[
5
]
]
];
I believe the reason why you were having trouble was because you were
using the syntax @$familyArrayRef[$$numFamilies]. This isn't what you
want; this is actually an array *slice* rather than a single element of an
array. That's because you have the @ out in front, which means you're
telling Perl you want an array (or list) back.
$$familyArrayRef[$$numFamilies] would have worked; the way I wrote it
above is probably clearer for this case.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 17 May 1999 18:15:38 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: negative subscripts
Message-Id: <yl675ruqit.fsf@windlord.stanford.edu>
Thomas Wade Vaughan <vaughan@well.com> writes:
> A co-worker wanted to know if we could start the index of an array with
> a negative value. Initially I said no, but later thought of changing the
> $[ variable to -5. This seemed to work until I put the array in a for
> loop and the behavior was what I thought a negative index should be. I
> mean the array was indexed from the end. Is this bug or what.?
No, negative array indexes are special-cased in most of Perl to mean that
many elements from the end of the array rather than the beginning.
Setting $[ to a negative value is likely to result in very confusing
results.
Use of $[ is discouraged, and the man page only explicitly mentions values
of 0 and 1. Other positive values would *probably* work, but nothing else
is likely to. If you really don't want to use a hash, you'll probably be
stuck adding an offset to your array accesses.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: Mon, 17 May 1999 18:07:30 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Parsing a data file...
Message-Id: <MPG.11aa5d255bb5d6ec989a89@nntp.hpl.hp.com>
In article <6%103.168$Ie1.11967@news14.ispnews.com> on Mon, 17 May 1999
23:54:42 GMT, dana@oz.net <dana@oz.net> says...
...
> @words = split / /, $_;
> $words[2] now contains the value you're looking for;
This is more efficient:
my $word3 = (split / /, $_, 4)[2];
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 17 May 1999 17:55:54 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <ylemkfurfp.fsf@windlord.stanford.edu>
John Porter <jdporter@min.net> writes:
> Russ Allbery <rra@stanford.edu> wrote:
>> C++ on the other hand gives no visual clues at all about such matters
>> in the actual code, making reading C++ code an exercise in multiple
>> windows to find all of the hidden gotchas and action at a distance.
> Man I wish you hadn't said this, Russ, because if anything, Perl is
> worse than C++ in this regard. At least with C++, the meaning of a
> program can be predicted by a lexical analysis of the code. But Perl
> has eval and other dynamic features:
> @Bar:ISA = ( time & 1 ) ? ('Foo') : ('Baz');
It depends on how you use Perl. (I suppose it also depends on how you use
C++, but most people seem to use it poorly and I don't think the same is
true of Perl.) Sure, dynamically changing @ISA is going to make things
really confusing. But on the other hand, Perl doesn't do everything via
operator overloading, so when you see a +, you have a reasonable idea of
what it actually does.
You can do some truly frighteningly obscure things in Perl, I agree. But
the vast majority of Perl object code out there is more straightforward
than C++ in my opinion. (One of the big reasons for that is that Perl's
loosely typed variables let you pretty much entirely avoid the nightmare
of templates.)
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 17 May 1999 18:00:06 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl to C translator?
Message-Id: <ylbtfjur8p.fsf@windlord.stanford.edu>
Sebastian Ahrens <dataadm@elfi2.rz.ruhr-uni-bochum.de> writes:
> perlcc is your friend (perldoc perlcc should bring up the man page)...
Note that perlcc may or may not work for your particularly application, as
it's still under active development and probably should be considered
incomplete alpha code. Note also that if you *really* need this for some
reason, the version in the current development track (_57 or so) is
probably more robust than the version in the current maintenance tracks.
On the other hand, Perl *itself* is considerably *less* robust in the
development tracks, so using those versions of Perl isn't recommended
unless you really badly need something there.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 17 May 1999 20:14:05 -0500
From: aray@nmds.com (Arjun Ray)
Subject: Re: Sorting problem
Message-Id: <37d3bfb7.2678908336@news1.newscene.com>
In <m13e0vpmte.fsf@halfdome.holdit.com>,
merlyn@stonehenge.com (Randal L. Schwartz) wrote:
| >>>>> "John" == John Porter <jdporter@min.net> writes:
| John> Use a kind of Schwartzian Transform thing:
|
| John> for (
| John> sort { $b->[2] <=> $a->[2] }
| John> map { [ split /;/ ] }
| John> @techlines
| John> ) {
| John> my( $type, $setnr, $technr, $subject, $s5, $s6, $s7, $note ) = [$_];
| John> ...
|
| That would seem to be more of a Porter-nization, rather than a
| Schwartzian Transform. :) Without that third map, you're not getting
| anywhere close to the canonical form. :)
Isn't it also (a philosphical?) part of the canonical form to eschew
temporaries? So, would this kind of Transform Thing, then, qualify as
an extension of the canonical form?
print
map { '<tr><td>'
. join( '</td><td>', map { /^$/ and "-" or $_ } @$_ )
. "</td></tr>\n" }
sort { $b->[2] <=> $a->[2] }
grep { $_->[1] =~ m/$FORM{'TNSET'}/i }
map { chop if /\n$/ ; [split /;/] }
(@techlines) ;
(I know I've got something wrong somewhere, but this is fun!)
:ar
------------------------------
Date: Mon, 17 May 1999 16:45:37 -0700
From: Phil Voris <pvorishatesspam@earthlink.net>
Subject: Re: Using split with map, then join
Message-Id: <3740AA21.82B80519@earthlink.net>
I would do this instead...
#!/usr/local/bin/perl -w
use strict;
use diagnostics;
open DAT, 'database3.txt' or die $!;
print map {(join '|', ((split /\|/)[0,3..6])), "\n"} <DAT>;
# file is auto-closed on script completion -- death may be a bit harsh.
------------------------------
Date: 18 May 1999 00:43:12 GMT
From: ada@fc.hp.com (Andrew Allen)
Subject: Re: Using split with map, then join
Message-Id: <7hqd30$d4e$1@fcnews.fc.hp.com>
lou@visca.com wrote:
: I'm trying to use map to select certain fields from a bar-separated flat
: file database and thus produce a smaller flat file database. After
: splitting, when I put the bars back in using join, I have to do a rather
: clumsy little substitution. I would be grateful if someone could suggest
: a better way.
Yes, I'd process the file line-by-line instead of slurping it in all
at once:
open DAT,$data or die "can't open file $data: $!";
while(<DAT>)
{
print join ',',(split(/\|/,$_))[0,3..6];
}
close DAT or die "can't close file $data: $!";
or if you have to slurp it in, then process each line separately:
...
@data=<DAT>;
...
print map {chomp; join("|",(split(/\|/,$_))[0,3..6])."\n"} @data;
Andrew
------------------------------
Date: Mon, 17 May 1999 17:53:07 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Using split with map, then join
Message-Id: <MPG.11aa59c8e05f2602989a88@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <7hq85h$78f$1@nnrp1.deja.com> on Mon, 17 May 1999 23:19:15
GMT, lou@visca.com <lou@visca.com> says...
> I'm trying to use map to select certain fields from a bar-separated flat
> file database and thus produce a smaller flat file database. After
> splitting, when I put the bars back in using join, I have to do a rather
> clumsy little substitution. I would be grateful if someone could suggest
> a better way. Here's a sample of the database:
>
> #1001|NRFB|820|Barbie in Mexico|NRFB|1|$250|A|02/19/99
> #1002|NRFB|785|Dreamboat|NRFB|1|$80|A|02/19/99
> #1004|NRFB|778|Ken in Mexico|NRFB|1|$175|A|02/19/99
...
> my @newdata = map {(split /\|/)[0,3..6], "\n"} @data;
> my $new = join '|', @newdata;
> $new =~ s/\|\n\|?/\n/g;
>
> print $new;
...
my @newdata = map join('|', (split /\|/)[0,3..6]) . "\n", @data;
print @newdata;
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 17 May 1999 18:29:16 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Using split with map, then join
Message-Id: <MPG.11aa62454f19c1f5989a8a@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <7hqd30$d4e$1@fcnews.fc.hp.com> on 18 May 1999 00:43:12 GMT,
Andrew Allen <ada@fc.hp.com> says...
...
> print join ',',(split(/\|/,$_))[0,3..6];
^
| (typo)
You've lost the "\n" on the end of each line (assuming more than 7
fields in each input line, as in the sample data set).
> print map {chomp; join("|",(split(/\|/,$_))[0,3..6])."\n"} @data;
Now you've put the newline back. However, the chomp is unnecessary
(assuming more than 7 fields in each input line, as in the sample data
set).
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Mon, 17 May 1999 18:41:25 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Using split with map, then join
Message-Id: <MPG.11aa651ba314d5e2989a8b@nntp.hpl.hp.com>
In article <ebohlmanFBwHLp.GEv@netcom.com> on Mon, 17 May 1999 23:37:01
GMT, Eric Bohlman <ebohlman@netcom.com> says...
> lou@visca.com wrote:
...
> : #1001|NRFB|820|Barbie in Mexico|NRFB|1|$250|A|02/19/99
> : #1002|NRFB|785|Dreamboat|NRFB|1|$80|A|02/19/99
> : #1004|NRFB|778|Ken in Mexico|NRFB|1|$175|A|02/19/99
...
> : my @newdata = map {(split /\|/)[0,3..6], "\n"} @data;
>
> You've already got a \n at the end of field 6, you don't need to add a
> new one.
>
> my @newdata=(split /\|/)[0,3..6];
>
> is all you need.
No -- the "\n" was at the end of field 8, which has gone bye-bye.
I think at least two of those who responded to this question forgot to
test their code. I had the same bug until I tested mine -- before
posting it. :-)
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 18 May 1999 00:48:45 GMT
From: Josh Pointer <josh@bitwell.net>
Subject: Your assistance humbly requested (file processing problem)
Message-Id: <3740B9C6.4401@bitwell.net>
Hi,
My problem is as follows. I need to read in an existing file, throw away
certain predefined lines and write the remaining lines to a new file
which replaces the existing file. Being overly blessed with hubris, I
thought I had it, but I apparently don't. The following code produces a
blank file. Having faithfully but fruitlessly perused the FAQ,
Programming Perl, Learning Perl, and the past two weeks' postings to
this group in search of enlightenment, I now throw myself on the mercy
of this distinguished body. Please be gentle. Thanks in advance.
Josh Pointer
# The values in @initialize correspond to the number of lines which are
needed (positives) and
# the number which aren't (negatives). These are ordered from the
beginning of the file.
# In other words, the first 27 lines need to go, the next 2 we keep, the
next 16 go, etc.
my(@initialize) = ( -27, 2, -16, 3, -8, 3, -8, 3, -10, 2, -8, 3, -9, 3,
-8, 3, -8, 3, -29 );
open(FILE_IN, $temp_file) or die "fatal error on open sub a 1";
open(FILE_OUT, ">${temp_file}.tmp") or die "fatal error on open sub a
2";
for ( $i = 0; $i < 19; $i++ ) { # cycle through @initialize
$length = $initialize[$i]; # pull the value
if ($length < 0) { # i.e., if we're throwing away these lines
$length *= -1; # make $length usable
for ( $j = 0; $j < $length; $j++) { # pull the corresponding number
of lines
<FILE_IN>; # throw them away
}
} else { # i.e., we're keeping these lines
for ( $j = 0; $j < $length; $j++) { # pull the corresponding number
of lines
$line = <FILE_IN>;
print FILE_OUT $line; # write them out
}
}
}
close(FILE_IN);
close(FILE_OUT);
rename("${temp_file}.tmp", $temp_file); # presto! same file, new
contents. (yeah,
# too bad it doesn't work...)
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
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 5706
**************************************