[17293] in Perl-Users-Digest
Perl-Users Digest, Issue: 4715 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 24 18:15:40 2000
Date: Tue, 24 Oct 2000 15:15:16 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <972425716-v9-i4715@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 24 Oct 2000 Volume: 9 Number: 4715
Today's topics:
Re: replacing known lines <lr@hpl.hp.com>
Re: replacing known lines <vandenb@NO.SPAMcistron.nl>
Re: replacing known lines <vandenb@NO.SPAMcistron.nl>
Re: replacing known lines <lr@hpl.hp.com>
Re: replacing known lines <tim@ipac.caltech.edu>
Re: replacing known lines <vandenb@NO.SPAMcistron.nl>
Re: replacing known lines <vandenb@NO.SPAMcistron.nl>
simple counter in a text file <workmonster2@home.com>
Re: simple counter in a text file <jihad.battikha@sharewire.com>
sorting multidimensional arrays exster@hotmail.com
Re: sorting multidimensional arrays <quantum_mechanic@my-deja.com>
Re: What will the code look like? <themoriman@ntlworld.com>
Re: What will the code look like? (Tad McClellan)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 24 Oct 2000 11:45:35 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: replacing known lines
Message-Id: <MPG.145f78ab5894ba998ae64@nntp.hpl.hp.com>
In article <8t4222$bf7$1@zonnetje.nl.uu.net> on Tue, 24 Oct 2000
15:14:36 +0200, walter <w.berg@samsom.nl> says...
> Hi group,
Hi.
> I've justed started with perl (here we go again), and I want to make a
> script that reads files in a directory and puts them in a certain context,
> like this:
>
>
> opendir(HERE, '.');
This should be tested for failure, like your next line. Make sure that
each diagnostic includes the reason for the failure, which is in $!.
> open(OUT, '>plaatjes.def') or die "Couldn't make the file...\n";
>
> @Allfiles = readdir(HERE);
> foreach $Name (@Allfiles) {
Using leading capitals for local variables is a bit unusual. I reserve
them for large-scope globals.
> print OUT '<OD:FO:"'."$Name".'",bitmap,"afbeeldingen/'."$Name".'">'."\n";
That is very hard to read, not least because of the superfluous quotes
around simple scalar variables. Compare this:
print OUT qq{<OD:FO:"$Name",bitmap,"afbeeldingen/$Name">\n};
Look in perlop for the alternate double-quoting operator qq().
> }
>
> And it works fine. Reason i use '.' is because it should be easy to work
> with for people who are scared of commandlines, so it's just: drop the
> script in the directory where the pictures are (we work with folioviews:
> http://www.nextpage.com/foliostuff/index.asp ), doubleclick it and get a
> file that's ready to use.
>
> Now the script also puts the following lines in the file:
>
> <OD:FO:".",bitmap,"afbeeldingen/.">
> <OD:FO:"..",bitmap,"afbeeldingen/..">
> <OD:FO:"fileReader.pl",bitmap,"afbeeldingen/fileReader.pl">
>
> (fileReader.pl is the name of my script)
>
> I know why these lines are in my file, but now I want them out, and my best
> guess was: use regular expressions. So I used:
>
> $empty = ""; #empty variable that's used in the next replacement
> s/<OD:FO:"\.",bitmap,"afbeeldingen\/\.">/$leeg/;
Is $leeg supposed to be $empty?
> And something similar for the other lines. But I can't find out how I put
> these lines in my script. Yes, i'm an idiot.
Do you really want empty lines in your output? It is more usual just to
filter the bad lines out completely.
foreach my $name (readdir HERE) {
next if $name eq '.'
|| $name eq '..'
|| $name eq 'fileReader.pl';
print OUT qq{<OD:FO:"$name",bitmap,"afbeeldingen/$name">\n};
}
That is very simple to read and to modify. Here are more compact ways
of doing the same thing, which may lead you into valuable new
adventures, such as 'grep':
print OUT qq{<OD:FO:"$_",bitmap,"afbeeldingen/$_">\n} for
grep !/^\.\.?\z/ && $_ ne 'fileReader.pl' => readdir HERE;
or 'map':
print OUT map !/^\.\.?\z/ && $_ ne 'fileReader.pl' &&
qq{<OD:FO:"$_",bitmap,"afbeeldingen/$_">\n} => readdir HERE;
> But thanks anyway.
My pleasure.
BTW, the above snippets haven't been tested, but they look all right to
me.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 24 Oct 2000 21:04:11 +0200
From: "Walter van den Berg" <vandenb@NO.SPAMcistron.nl>
Subject: Re: replacing known lines
Message-Id: <8t4meg$3h3$1@voyager.cistron.net>
"Larry Rosler" <lr@hpl.hp.com> wrote in message
> > $empty = ""; #empty variable that's used in the next replacement
> > s/<OD:FO:"\.",bitmap,"afbeeldingen\/\.">/$leeg/;
>
> Is $leeg supposed to be $empty?
Oops - yes. i tried to "Englishfy" my Dutch while pasting my code into the
message, but i forgot that one. Empty and leeg are exactly the same words.
Thank you for your help!
Walter.
--
http://vandenb.com
------------------------------
Date: Tue, 24 Oct 2000 21:58:12 +0200
From: "Walter van den Berg" <vandenb@NO.SPAMcistron.nl>
Subject: Re: replacing known lines
Message-Id: <8t4pjq$h5g$1@voyager.cistron.net>
I wrote:
> Hi group,
<snip myself>
> $empty = ""; #empty variable that's used in the next replacement
> s/<OD:FO:"\.",bitmap,"afbeeldingen\/\.">/$empty/;
>
> I can't find out how I put
> these lines in my script. Yes, i'm an idiot.
I knew that I'm an idiot. Larry answered my question, really, the script
works, but I was secretly hoping to get this part of the question answered.
So:
I really like to know how a script looks that opens a file, changes
something in the content, and closes (saves) it. This must sound stupid
(flamebait, I know) but I cannot find a clear example of it.
Again:
open
change
close.
That's it.
Thanks.
------------------------------
Date: Tue, 24 Oct 2000 13:14:21 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: replacing known lines
Message-Id: <MPG.145f8d7987b34a9998ae69@nntp.hpl.hp.com>
In article <8t4pjq$h5g$1@voyager.cistron.net> on Tue, 24 Oct 2000
21:58:12 +0200, Walter van den Berg <vandenb@NO.SPAMcistron.nl> says...
...
> I really like to know how a script looks that opens a file, changes
> something in the content, and closes (saves) it. This must sound stupid
> (flamebait, I know) but I cannot find a clear example of it.
> Again:
> open
> change
> close.
> That's it.
Except in special circumstances, you cannot change lines directly in the
file you are reading, but must write to a second file.
You will find a simple example in perlfaq5: "How do I change one line in
a file/delete a line in a file/insert a line in the middle of a
file/append to the beginning of a file"
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 24 Oct 2000 13:19:18 -0700
From: Tim Conrow <tim@ipac.caltech.edu>
Subject: Re: replacing known lines
Message-Id: <39F5EEC6.227FBA35@ipac.caltech.edu>
walter wrote:
> I've justed started with perl (here we go again),
Then start learning Perl by starting your scripts off right:
use strict;
use warnings;
This makes you work just a little harder, but rewards you with a great deal of
useful information about what may be wrong with your script.
> and I want to make a
> script that reads files in a directory and puts them in a certain context,
> like this:
>
> opendir(HERE, '.');
Why no error test? It's possible for an error to occur opening a directory just
as for opening a file.
opendir(HERE, '.') or die "Error opening .; $!\n";
> open(OUT, '>plaatjes.def') or die "Couldn't make the file...\n";
Good, an error test. But add $! to the error message. See perlvar.
>
> @Allfiles = readdir(HERE);
Put 'my' infront of that line. See perlfunc. Also, get used to reserving
capitalized var.s for special global var.s. Once you put 'my' in front, the var.
is lexical; local to this file.
> foreach $Name (@Allfiles) {
foreach my $name (@Allfiles) {
>
> print OUT '<OD:FO:"'."$Name".'",bitmap,"afbeeldingen/'."$Name".'">'."\n";
> }
>
> And it works fine. Reason i use '.' is because it should be easy to work
> with for people who are scared of commandlines, so it's just: drop the
> script in the directory where the pictures are (we work with folioviews:
> http://www.nextpage.com/foliostuff/index.asp ), doubleclick it and get a
> file that's ready to use.
Well, that's your call, but it's a messy business, as you've found. I'm sure
there's a better way where everyone can use the same script and you don't end up
with a zillion copies of your possibly out of date, error prone script laying
all over the place. Try to come up with a better way.
>
> Now the script also puts the following lines in the file:
>
> <OD:FO:".",bitmap,"afbeeldingen/.">
> <OD:FO:"..",bitmap,"afbeeldingen/..">
> <OD:FO:"fileReader.pl",bitmap,"afbeeldingen/fileReader.pl">
>
> (fileReader.pl is the name of my script)
>
> I know why these lines are in my file, but now I want them out, and my
> best guess was: use regular expressions. So I used:
>
> $empty = ""; #empty variable that's used in the next replacement
> s/<OD:FO:"\.",bitmap,"afbeeldingen\/\.">/$leeg/;
These lines are sufficiently mysterious that I'm not going to comment. :-)
Instead, I'll suggest this code and let you figure it out (untested):
my @allfiles = readdir(HERE);
foreach my $name (grep ! /^\.\.?$/ and $0 !~ /$_$/, @allfiles) {
print OUT qq@<OD:FO:"$name",bitmap,"afbeeldingen/$name">\n@;
}
See perlvar for $0, perlfunc for qq and grep and perlre and perlop for
'!~ / whatever /'.
--
-- Tim Conrow tim@ipac.caltech.edu |
------------------------------
Date: Tue, 24 Oct 2000 23:03:16 +0200
From: "Walter van den Berg" <vandenb@NO.SPAMcistron.nl>
Subject: Re: replacing known lines
Message-Id: <8t4tdq$hl$1@voyager.cistron.net>
"Larry Rosler" <lr@hpl.hp.com> wrote
> Except in special circumstances, you cannot change lines directly in the
> file you are reading, but must write to a second file.
>
> You will find a simple example in perlfaq5: "How do I change one line in
> a file/delete a line in a file/insert a line in the middle of a
> file/append to the beginning of a file"
Found it. *blush*
I will read the perlfaq better.
I will read the perlfaq better.
I will read the perlfaq better.
I will read the perlfaq better.
Walter.
--
http://vandenb.com
------------------------------
Date: Tue, 24 Oct 2000 23:07:22 +0200
From: "Walter van den Berg" <vandenb@NO.SPAMcistron.nl>
Subject: Re: replacing known lines
Message-Id: <8t4tlg$1dk$1@voyager.cistron.net>
"Tim Conrow" <tim@ipac.caltech.edu> wrote
> > @Allfiles = readdir(HERE);
>
> Put 'my' infront of that line. See perlfunc. Also, get used to reserving
> capitalized var.s for special global var.s.
Mm - I'm not sure if I should say this here, but I used an example that was
in "perl for dummies". And the capitalized var was there.
No flames please! - I later bought "learning perl" from O'Reilly.
Walter.
--
http://vandenb.com
------------------------------
Date: Tue, 24 Oct 2000 19:10:06 GMT
From: "kbent" <workmonster2@home.com>
Subject: simple counter in a text file
Message-Id: <i8lJ5.511881$8u4.6481254@news1.rdc1.bc.home.com>
Greetings from a perl-scripting wannabe;
I'm looking for a simple perl script to count pages, as incremented in a
separate text file. Every time the browser refreshes, I would like the
script to increment the data in the text file, and save it.
I've fumbled with the following script a bit, but no real success - If
anyone has any suggestions on repairing the script below, or a location to
find a perl-based counter, I would appreciate hearing from you.
Thank You
Kev
--
www.workmonster.bc.ca
#!c:\perl\bin\perl.exe
use CGI qw(:standard);
print header;
print start_html;
open (BAG, "file.txt"); #supposed to open the file handle BAG and link it
to a text file.
$contents=(BAG); #supposed to put the contents of BAG into a variable
print "$contents\n\n"; #just checking what's in the file
($contents=++$contents); #increments the contents of the variable
print "$contents\n\n"; #prints the new contents
$watch=++$contents; #increments the contents again, and puts 'em into
another variable
print "$watch\n\n"; #prints the new contents
print (BAG $contents); #supposed to write the new incrementation to the file
print h1($watch); #a final print of the contents.
close BOX;
--
www.workmonster.bc.ca
------------------------------
Date: Tue, 24 Oct 2000 17:59:34 -0400
From: Jihad Battikha <jihad.battikha@sharewire.com>
Subject: Re: simple counter in a text file
Message-Id: <39F60646.9136B9BE@sharewire.com>
kbent wrote:
> I've fumbled with the following script a bit, but no real success
> ....
> open (BAG, "file.txt"); #supposed to open the file handle BAG and
> link it to a text file.
That's fine, but you should also check for error condistions if the file
didn't open.
open (BAG, "file.txt") || die $!;
$! will contain the error string. Since your script is for a CGI
environment, you might want to encapsulate the error in another
subroutine. Anyway, you're using the CGI module so let's ignore that
issue for now...
> $contents=(BAG); #supposed to put the contents of BAG into a variable
That's not going to work. Try:
$contents = <BAG>;
That will suck in the first "line" of the ASCII text file assuming your
record separator global ($/) is set to "\n". If you want to suck in a
mult-line file all into one variable, you would do something similar to:
open (BAG, "file.txt") || die $!;
my $contents;
{
local $/;
$contents = <BAG>;
}
close(BAG);
or, you could avoid playing with $/ and use a temporary array:
open (BAG, "file.txt") || die $!;
my @contents = (<BAG>);
my $contents = join('', @contents);
close(BAG);
> print "$contents\n\n"; #just checking what's in the file
> ($contents=++$contents); #increments the contents of the variable
Simpler:
$contents++;
(equiv. to $contents = $contents + 1)
> ....
> print (BAG $contents); #supposed to write the new
> incrementation to the file
No, you opened the file with an implied *read* so printing to it will
fail. You must first close the file and then re-open it to do an
explicit write. Example:
close(BAG);
open(BAG, ">file.txt") || die $!;
Then you can print to the file. Everything else in the file will get
wiped out so you may want to open it for simultaneous read/write. Read
the Perl docs on how to do that.
> print h1($watch); #a final print of the contents.
You're not checking to see if your file write actually worked - all
you're doing is printing a variable you changed, not the success of the
write. All you're going to see is the same number over-and-over, no
matter how many times you reload the page.
> close BOX;
Where did that handle come from? You didn't declare it earlier with a
proper discriptor...
Also, you should look into file locking whenever you're eading from or
writing to a potentially shared file like a hit counter database.
--
Jihad Battikha <jihad.battikha@sharewire.com>
Sharewire, Inc. --- http://www.sharewire.com/
- Free forms, programs, and content for web sites.
- No assembly required.
Disclaimer:
Before sending me commercial e-mail, the sender must first agree
to my LEGAL NOTICE located at: http://www.highsynth.com/sig.html
------------------------------
Date: Tue, 24 Oct 2000 18:17:27 GMT
From: exster@hotmail.com
Subject: sorting multidimensional arrays
Message-Id: <8t4jne$i2m$1@nnrp1.deja.com>
I have read through Larry Wall's book and the perl faqs and am still
stumped.
I have an array ...
@ARRAY = ("text2", "text1", "text3");
Then I have another array
while ($x<=5) {
@outer[@ARRAY[$x]]= $x*$x;
$x++;
}
So that "text2" corresponds to "x^2" and "text3" corresponds to "x^3"
and so forth.
I want to sort the outer array while keeping the corresponding values of
"text2 to x^2" and "text3 to x^3" intact.
How do I do that using the sort {$a <=> $b) function?
Thanks
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Tue, 24 Oct 2000 19:11:49 GMT
From: Quantum Mechanic <quantum_mechanic@my-deja.com>
Subject: Re: sorting multidimensional arrays
Message-Id: <8t4mte$ks9$1@nnrp1.deja.com>
Since I haven't seen a reply yet, here's an attempt...
In article <8t4jne$i2m$1@nnrp1.deja.com>,
exster@hotmail.com wrote:
> @ARRAY = ("text2", "text1", "text3");
>
<snip>
>
> @outer[@ARRAY[$x]]= $x*$x;
<snip>
>
> I want to sort the outer array while keeping the corresponding values
of
> "text2 to x^2" and "text3 to x^3" intact.
>
> How do I do that using the sort {$a <=> $b) function?
>
First, you are mixing up your type indicators ($, @, %). A scalar
variable reference begins with '$', an array with '@', and a hash
with '%'. However, if you are referencing a scalar *element* in an
array or hash, use '$' with the array name.
For example, to assign to a single array element:
$array[27] = "something";
The (square) brackets indicate that 'array' is an array variable.
Similarly, (curly) braces indicate a hash variable, as in:
$hash{32} = "something else";
Second, you probably want a hash for the second array. Using your
example:
while ($x <= $5)
{
$outer{$ARRAY{$x}} = $x*$x;
$x++;
}
There are numerous ways to do this with less code, such as:
@inner = qw( zero, one, two, three, four, five );
%outer = map { $inner[$_], $_*$_ } (0..$#inner)
map { print "\$outer{$_} = <$outer{$_}>\n" } keys %outer;
gives:
$outer{zero} = <0>
$outer{five} = <25>
$outer{two} = <4>
$outer{one} = <1>
$outer{four} = <16>
[Not in order because the hash is not stored in order.]
See pp. 6-7 of Programming Perl (2e), for instance.
-QM
--
Quantum Mechanics: The dreams stuff is made of.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Tue, 24 Oct 2000 20:00:10 +0100
From: "The Moriman" <themoriman@ntlworld.com>
Subject: Re: What will the code look like?
Message-Id: <FZkJ5.10836$Rk5.248733@news2-win.server.ntlworld.com>
Russ Jones <russ_jones@rac.ray.com> wrote in message
news:39F59A1E.4F2588DF@rac.ray.com...
> Gwyn Judd wrote:
> >
> > I was shocked! How could Tad McClellan <tadmc@metronet.com>
> > say such a terrible thing:
> >
> > >Who is Spike Milligan, and what has he done with poor Ogden Nash?
> >
> > Spike Milligan was one of the Goon Show members. Who the heck was Ogden
> > Nash?
> >
>
> Jeez Louise I've fallen amongst the Philistines.
>
> --
Hey! Less of the insults!
I'm not from Philister ;-)
TMMan
------------------------------
Date: Tue, 24 Oct 2000 16:45:53 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: What will the code look like?
Message-Id: <slrn8vbt81.956.tadmc@magna.metronet.com>
On Tue, 24 Oct 2000 20:00:10 +0100, The Moriman <themoriman@ntlworld.com> wrote:
>
>Russ Jones <russ_jones@rac.ray.com> wrote in message
>news:39F59A1E.4F2588DF@rac.ray.com...
>> Gwyn Judd wrote:
>> >
>> > I was shocked! How could Tad McClellan <tadmc@metronet.com>
>> > say such a terrible thing:
>> >
>> > >Who is Spike Milligan, and what has he done with poor Ogden Nash?
>> >
>> > Spike Milligan was one of the Goon Show members. Who the heck was Ogden
>> > Nash?
>> >
>>
>> Jeez Louise I've fallen amongst the Philistines.
>>
>> --
>
>Hey! Less of the insults!
>I'm not from Philister ;-)
Errr, I thought he meant me, for not knowing who Spike was...
High fan-out on that insult, heh heh.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V9 Issue 4715
**************************************