[28850] in Perl-Users-Digest
Perl-Users Digest, Issue: 94 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 1 21:06:26 2007
Date: Thu, 1 Feb 2007 18:05:13 -0800 (PST)
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, 1 Feb 2007 Volume: 11 Number: 94
Today's topics:
perl one liner to remove all but the base file name <oxnard@carolina.rr.com>
Re: perl one liner to remove all but the base file name <mritty@gmail.com>
Re: perl one liner to remove all but the base file name <abigail@abigail.be>
Randomly Choose from an Array cylurian@gmail.com
Re: Randomly Choose from an Array <glennj@ncf.ca>
Re: Randomly Choose from an Array <bik.mido@tiscalinet.it>
Re: Randomly Choose from an Array <wahab-mail@gmx.de>
Re: Randomly Choose from an Array <abigail@abigail.be>
Re: Randomly Choose from an Array <wahab-mail@gmx.de>
Re: Randomly Choose from an Array <purlgurl@purlgurl.net>
Re: Randomly Choose from an Array <abigail@abigail.be>
Read on closed filehandle <Russ.Dilley@gmail.com>
Re: Read on closed filehandle <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Read on closed filehandle <Russ.Dilley@gmail.com>
Re: Read on closed filehandle <attn.steven.kuo@gmail.com>
Re: Read on closed filehandle <bik.mido@tiscalinet.it>
Re: Read on closed filehandle <Russ.Dilley@gmail.com>
Re: Read on closed filehandle jgraber@ti.com
Re: Read on closed filehandle jgraber@ti.com
Re: Read on closed filehandle <tadmc@augustmail.com>
Re: Read on closed filehandle <jgibson@mail.arc.nasa.gov>
Re: Read on closed filehandle <damercer@comcast.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 1 Feb 2007 19:21:40 -0500
From: "Oxnard" <oxnard@carolina.rr.com>
Subject: perl one liner to remove all but the base file name
Message-Id: <45c28416$0$16668$4c368faf@roadrunner.com>
If I have a file name like:
/dkdk888/jdty/file.txt
how can I get to just 'file' using a regex. I have come up with
or
/dkdk/dkdue/dfdf.txt.1234
and ending up with 'dfdf'
I have come up with 's/^.*\/\([^/]*\)$/\1/' but cannot get rid of the
extension. Also I have thought about using basename with the suffix but the
suffix can be different.
Thanks
------------------------------
Date: 1 Feb 2007 16:50:27 -0800
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: perl one liner to remove all but the base file name
Message-Id: <1170377426.645690.142710@k78g2000cwa.googlegroups.com>
On Feb 1, 7:21 pm, "Oxnard" <oxn...@carolina.rr.com> wrote:
> If I have a file name like:
>
> /dkdk888/jdty/file.txt
>
> how can I get to just 'file' using a regex. I have come up with
> or
> /dkdk/dkdue/dfdf.txt.1234
>
> and ending up with 'dfdf'
>
> I have come up with 's/^.*\/\([^/]*\)$/\1/' but cannot get rid of the
> extension. Also I have thought about using basename with the suffix but the
> suffix can be different.
That's not your real regexp. Please do not retype code. Copy and
paste.
Why do you have a requirement for a regexp? Unless you have a good
reason to impose that artificial requirement, this is the correct
solution:
perl -MFile::Basename -le'print scalar fileparse($ARGV[0], qr/\..*/)' /
dkdk/dkdue/dfdf.txt.1234
Paul Lalli
------------------------------
Date: 02 Feb 2007 00:59:19 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: perl one liner to remove all but the base file name
Message-Id: <slrnes536r.ekq.abigail@alexandra.abigail.be>
Oxnard (oxnard@carolina.rr.com) wrote on MMMMCMIII September MCMXCIII in
<URL:news:45c28416$0$16668$4c368faf@roadrunner.com>:
&& If I have a file name like:
&&
&& /dkdk888/jdty/file.txt
&&
&& how can I get to just 'file' using a regex. I have come up with
&& or
&& /dkdk/dkdue/dfdf.txt.1234
&&
&& and ending up with 'dfdf'
&&
&& I have come up with 's/^.*\/\([^/]*\)$/\1/' but cannot get rid of the
&& extension. Also I have thought about using basename with the suffix but the
&& suffix can be different.
s !.*/!!; s !\.[^.]+$!!;
Abigail
--
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
.qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
.qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'
------------------------------
Date: 1 Feb 2007 11:54:30 -0800
From: cylurian@gmail.com
Subject: Randomly Choose from an Array
Message-Id: <1170359670.518755.284590@a34g2000cwb.googlegroups.com>
Hello everyone. How would I be able to choose randomly elements from
an array?
@r = (1, 2, 3, 4)
Randomly choose numbers and put back in the array
@r = (4, 2, 1, 3);
$p = $r[0]
print $p
------------------------------
Date: 1 Feb 2007 20:02:41 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Randomly Choose from an Array
Message-Id: <slrnes4hr2.o5h.glennj@smeagol.ncf.ca>
At 2007-02-01 02:54PM, "cylurian@gmail.com" wrote:
> Hello everyone. How would I be able to choose randomly elements from
> an array?
>
> @r = (1, 2, 3, 4)
>
> Randomly choose numbers and put back in the array
>
> @r = (4, 2, 1, 3);
use List::Util qw(shuffle);
my @p = (1,2,3,4);
my @r = shuffle @p;
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
------------------------------
Date: Thu, 01 Feb 2007 21:20:58 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Randomly Choose from an Array
Message-Id: <tni4s2l6vtsa3ccj9blg9cjlibf77u96i9@4ax.com>
On 1 Feb 2007 11:54:30 -0800, cylurian@gmail.com wrote:
>Hello everyone. How would I be able to choose randomly elements from
>an array?
>
>@r = (1, 2, 3, 4)
$r[rand @r];
>Randomly choose numbers and put back in the array
>
>@r = (4, 2, 1, 3);
A different question:
perldoc -q shuffle
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Thu, 01 Feb 2007 21:23:35 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Randomly Choose from an Array
Message-Id: <eptih6$7n5$1@mlucom4.urz.uni-halle.de>
cylurian@gmail.com wrote:
> Hello everyone. How would I be able to choose
> randomly elements from an array?
> @r = (1, 2, 3, 4)
OK, this is rather simple:
...
@r = (1, 2, 3, 4);
$p = $r[ int rand @r ];
print $p;
> Randomly choose numbers and put back in the array
> @r = (4, 2, 1, 3);
> $p = $r[0]
> print $p
You could use 'shuffle' for that (as Glenn already said),
but also could do without:
...
@r = (1, 2, 3, 4);
@r = do {my @p; push @p, splice @r,int rand @r,1 while @r; @p};
print "$r[0] (@r)";
Regards
M.
------------------------------
Date: 01 Feb 2007 22:28:38 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Randomly Choose from an Array
Message-Id: <slrnes4qca.ekq.abigail@alexandra.abigail.be>
Mirco Wahab (wahab-mail@gmx.de) wrote on MMMMCMII September MCMXCIII in
<URL:news:eptih6$7n5$1@mlucom4.urz.uni-halle.de>:
""
"" You could use 'shuffle' for that (as Glenn already said),
"" but also could do without:
""
"" ...
"" @r = (1, 2, 3, 4);
""
"" @r = do {my @p; push @p, splice @r,int rand @r,1 while @r; @p};
"" print "$r[0] (@r)";
""
Yes, you could. But why would you want to use a quadratic algorithm
instead of a linear one?
Abigail
--
perl -wle 'eval {die [[qq [Just another Perl Hacker]]]};; print
${${${@}}[$#{@{${@}}}]}[$#{${@{${@}}}[$#{@{${@}}}]}]'
------------------------------
Date: Fri, 02 Feb 2007 00:05:53 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Randomly Choose from an Array
Message-Id: <epts1r$apa$1@mlucom4.urz.uni-halle.de>
Abigail wrote:
> Mirco Wahab (wahab-mail@gmx.de) wrote on MMMMCMII September MCMXCIII in
> <URL:news:eptih6$7n5$1@mlucom4.urz.uni-halle.de>:
> "" You could use 'shuffle' for that (as Glenn already said),
> "" but also could do without:
> "" @r = (1, 2, 3, 4);
> "" @r = do {my @p; push @p, splice @r,int rand @r,1 while @r; @p};
> "" print "$r[0] (@r)";
>
> Yes, you could. But why would you want to use a quadratic algorithm
> instead of a linear one?
Oops, I didn't really feel it would come out O(2) because I
thought the splice would find its elements in constant time.
But to be honest, I tried to give something like an 'alternative
formulation' for the 'shuffle', and the trivial solution like
my @r = (1, 2, 3, 4);
@r[$_->[0], $_->[1]] = @r[$_->[1], $_->[0]] for map [$_, int rand @r], 0..$#r;
was too much line noise (and some characters longer) ;-)
Thanks,
Mirco
------------------------------
Date: Thu, 01 Feb 2007 16:04:03 -0800
From: Purl Gurl <purlgurl@purlgurl.net>
Subject: Re: Randomly Choose from an Array
Message-Id: <45C27FF3.5090706@purlgurl.net>
cylurian@gmail.com wrote:
> How would I be able to choose randomly elements from an array?
> @r = (1, 2, 3, 4)
> Randomly choose numbers and put back in the array
> @r = (4, 2, 1, 3);
> $p = $r[0]
Your article is near gibberish.
Randomly selecting an array element is easy.
Inserting a new element is equally easy.
However, you state,
"...choose numbers and put back in the array...."
You have not removed elements from your array. What
numbers are there to "put back in" your array?
Your article is truly basic gibberish. You need to
state precisely your task at hand. Work towards
writing articles which are clear, concise and
cohesive, so readers do not have to guess.
Randomly select an array element,
$random = $Array[rand(@Array)];
Insert a random element from the same array,
push (@Array, $Array[rand(@Array)]);
Purl Gurl
------------------------------
Date: 02 Feb 2007 00:33:02 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Randomly Choose from an Array
Message-Id: <slrnes51li.ekq.abigail@alexandra.abigail.be>
Mirco Wahab (wahab-mail@gmx.de) wrote on MMMMCMII September MCMXCIII in
<URL:news:epts1r$apa$1@mlucom4.urz.uni-halle.de>:
@@ Abigail wrote:
@@ > Mirco Wahab (wahab-mail@gmx.de) wrote on MMMMCMII September MCMXCIII in
@@ > <URL:news:eptih6$7n5$1@mlucom4.urz.uni-halle.de>:
@@ > "" You could use 'shuffle' for that (as Glenn already said),
@@ > "" but also could do without:
@@ > "" @r = (1, 2, 3, 4);
@@ > "" @r = do {my @p; push @p, splice @r,int rand @r,1 while @r; @p};
@@ > "" print "$r[0] (@r)";
@@ >
@@ > Yes, you could. But why would you want to use a quadratic algorithm
@@ > instead of a linear one?
@@
@@ Oops, I didn't really feel it would come out O(2) because I
@@ thought the splice would find its elements in constant time.
Oh, it will *find* the element in constant time.
It's just that deleting an element and shifting the other elements
that's taking a long time (linear in the lenght of the array).
@@ But to be honest, I tried to give something like an 'alternative
@@ formulation' for the 'shuffle', and the trivial solution like
@@
@@ my @r = (1, 2, 3, 4);
@@
@@ @r[$_->[0], $_->[1]] = @r[$_->[1], $_->[0]] for map [$_, int rand @r], 0..$#r;
That's not a fair shuffle:
#!/usr/bin/perl
use warnings;
use strict;
no warnings 'syntax';
my $MAX = 24_000;
my %count;
for (1 .. $MAX) {
my @r = (1 .. 4);
@r[$_->[0], $_->[1]] = @r[$_->[1], $_->[0]]
for map [$_, int rand @r], 0..$#r;
$count {"@r"} ++;
}
foreach my $key (sort keys %count) {
printf "%s: %4d\n" => $key, $count {$key};
}
__END__
1 2 3 4: 977
1 2 4 3: 926
1 3 2 4: 908
1 3 4 2: 1342
1 4 2 3: 1006
1 4 3 2: 866
2 1 3 4: 906
2 1 4 3: 1424
2 3 1 4: 1254
2 3 4 1: 1308
2 4 1 3: 1069
2 4 3 1: 964
3 1 2 4: 1030
3 1 4 2: 1008
3 2 1 4: 832
3 2 4 1: 1032
3 4 1 2: 1033
3 4 2 1: 981
4 1 2 3: 720
4 1 3 2: 872
4 2 1 3: 873
4 2 3 1: 754
4 3 1 2: 947
4 3 2 1: 968
@@ was too much line noise (and some characters longer) ;-)
Shorter, and fair:
$r=@r;$i=rand$r--,@r[$i,$r]=@r[$r,$i]while$r;
#!/usr/bin/perl
use warnings;
use strict;
no warnings 'syntax';
my $MAX = 24_000;
my %count;
my ($r, $i);
for (1 .. $MAX) {
my @r = (1 .. 4);
$r=@r;$i=rand$r--,@r[$i,$r]=@r[$r,$i]while$r;
$count {"@r"} ++;
}
foreach my $key (sort keys %count) {
printf "%s: %4d\n" => $key, $count {$key};
}
__END__
1 2 3 4: 1039
1 2 4 3: 1019
1 3 2 4: 1000
1 3 4 2: 978
1 4 2 3: 988
1 4 3 2: 997
2 1 3 4: 1023
2 1 4 3: 957
2 3 1 4: 1024
2 3 4 1: 1034
2 4 1 3: 966
2 4 3 1: 1038
3 1 2 4: 945
3 1 4 2: 977
3 2 1 4: 1014
3 2 4 1: 973
3 4 1 2: 1003
3 4 2 1: 1030
4 1 2 3: 1005
4 1 3 2: 953
4 2 1 3: 1078
4 2 3 1: 975
4 3 1 2: 1043
4 3 2 1: 941
Abigail
--
my $qr = qr/^.+?(;).+?\1|;Just another Perl Hacker;|;.+$/;
$qr =~ s/$qr//g;
print $qr, "\n";
------------------------------
Date: 1 Feb 2007 14:34:08 -0800
From: "Russ" <Russ.Dilley@gmail.com>
Subject: Read on closed filehandle
Message-Id: <1170369248.849256.184780@m58g2000cwm.googlegroups.com>
I have a very simple script to read an input file, skip everything
between the strings "BEGIN" and "END", and store the results in an
output
file.
----------------------------
3 open(IN,"input.txt");
4 open(OUT,">output.txt");
5
6 while(<IN>) {
7 chomp;
8 if (/BEGIN/) {
9 until (/END/) {
10 $_ = <IN>;
11 }
12 } else {
13 print OUT "$_\n";
14 }
15 }
16
17 close IN;
18 close OUT;
----------------------------------
When I run this on a small file, about 28 kb, it works fine. When I
try it on a large file, about 3 gigs , I get the error:
"Read on closed filehandle <IN> at trim.pl line 6."
The file structure is essentailly the same, the only difference is the
size.
I bump into limits in many text editors when I try to access the large
file, and
I assume that this is a similar limitation in PERL. Is there any way
around this?
I also wouldn't mind some constructive criticism on the script
itself. It seems like there should
be a more elegant way to accomplish this task which might avoid this
problem.
Any suggestions?
Thanks,
Russ
------------------------------
Date: Thu, 01 Feb 2007 23:40:54 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Read on closed filehandle
Message-Id: <45c26c76$0$25936$ba4acef3@news.orange.fr>
Russ wrote:
> I have a very simple script to read an input file, skip everything
> between the strings "BEGIN" and "END", and store the results in an
> output
> file.
>
> ----------------------------
>
> 3 open(IN,"input.txt");
> 4 open(OUT,">output.txt");
> 5
> 6 while(<IN>) {
> 7 chomp;
> 8 if (/BEGIN/) {
> 9 until (/END/) {
> 10 $_ = <IN>;
> 11 }
> 12 } else {
> 13 print OUT "$_\n";
> 14 }
> 15 }
> 16
> 17 close IN;
> 18 close OUT;
>
> ----------------------------------
>
> When I run this on a small file, about 28 kb, it works fine. When I
> try it on a large file, about 3 gigs , I get the error:
>
> "Read on closed filehandle <IN> at trim.pl line 6."
It's best if you post code without the line numbers: makes it easier for
people to run it. You also need to run with
use strict;
use warnings;
and check the return value of your system calls (you need to do this
*always*), eg
open (my $infh,"<","input.txt") or die $!;
That should give you some pointers. perldoc -q open
Also check what
perl -V
reports for uselargefiles.
Mark
------------------------------
Date: 1 Feb 2007 15:03:58 -0800
From: "Russ" <Russ.Dilley@gmail.com>
Subject: Re: Read on closed filehandle
Message-Id: <1170371038.315841.252420@m58g2000cwm.googlegroups.com>
On Feb 1, 5:40 pm, Mark Clements <mark.clementsREMOVET...@wanadoo.fr>
wrote:
> use strict;
> use warnings;
>
> and check the return value of your system calls (you need to do this
> *always*), eg
>
> open (my $infh,"<","input.txt") or die $!;
>
> That should give you some pointers. perldoc -q open
>
> Also check what
>
> perl -V
>
> reports for uselargefiles.
I made the changes you suggested. The program dies at the open()
statement with error:
"Value too large for defined data type at trim.pl line 5."
Also, 'perl -V' does not reference uselargefiles. Is that something
that is set when perl in
initially compiled on the system? Does perl need to be recompiled to
set this value?
Thanks,
Russ
------------------------------
Date: 1 Feb 2007 15:10:19 -0800
From: "attn.steven.kuo@gmail.com" <attn.steven.kuo@gmail.com>
Subject: Re: Read on closed filehandle
Message-Id: <1170371412.186888.127590@j27g2000cwj.googlegroups.com>
On Feb 1, 2:34 pm, "Russ" <Russ.Dil...@gmail.com> wrote:
> I have a very simple script to read an input file, skip everything
> between the strings "BEGIN" and "END", and store the results in an
> output
> file.
>
> ----------------------------
>
> 3 open(IN,"input.txt");
Please check the return value of open.
> 4 open(OUT,">output.txt");
> 5
> 6 while(<IN>) {
> 7 chomp;
No need to chomp if you're going to append
"\n" before printing
> 8 if (/BEGIN/) {
> 9 until (/END/) {
> 10 $_ = <IN>;
> 11 }
What happends if your file
matches /BEGIN/ but never
matches /END/?
You keep reading, even
after you reach the end
of the file.
> 12 } else {
> 13 print OUT "$_\n";
> 14 }
> 15 }
> 16
> 17 close IN;
> 18 close OUT;
>
> ----------------------------------
>
> When I run this on a small file, about 28 kb, it works fine. When I
> try it on a large file, about 3 gigs , I get the error:
>
> "Read on closed filehandle <IN> at trim.pl line 6."
>
> The file structure is essentailly the same, the only difference is the
> size.
> I bump into limits in many text editors when I try to access the large
> file, and
> I assume that this is a similar limitation in PERL. Is there any way
> around this?
>
> I also wouldn't mind some constructive criticism on the script
> itself. It seems like there should
> be a more elegant way to accomplish this task which might avoid this
> problem.
> Any suggestions?
More concise:
while (<IN>) {
next if /BEGIN/ .. /END/;
print OUT $_;
}
--
Hope this helps,
Steven
------------------------------
Date: Fri, 02 Feb 2007 00:47:29 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Read on closed filehandle
Message-Id: <0uu4s2dol21kuv674ock2scj07m4tmtco2@4ax.com>
On 1 Feb 2007 14:34:08 -0800, "Russ" <Russ.Dilley@gmail.com> wrote:
>I have a very simple script to read an input file, skip everything
>between the strings "BEGIN" and "END", and store the results in an
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(Others explained to you what is wrong with your code, but in any
case) C<..> is your friend!
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: 1 Feb 2007 16:31:35 -0800
From: "Russ" <Russ.Dilley@gmail.com>
Subject: Re: Read on closed filehandle
Message-Id: <1170376295.591550.133920@a75g2000cwd.googlegroups.com>
On Feb 1, 6:10 pm, "attn.steven....@gmail.com"
<attn.steven....@gmail.com> wrote:
> On Feb 1, 2:34 pm, "Russ" <Russ.Dil...@gmail.com> wrote:
>
> > I have a very simple script to read an input file, skip everything
> > between the strings "BEGIN" and "END", and store the results in an
> > output
> > file.
>
> > ----------------------------
>
> > 3 open(IN,"input.txt");
>
> Please check the return value of open.
>
> > 4 open(OUT,">output.txt");
> > 5
> > 6 while(<IN>) {
> > 7 chomp;
>
> No need to chomp if you're going to append
> "\n" before printing
>
> > 8 if (/BEGIN/) {
> > 9 until (/END/) {
> > 10 $_ = <IN>;
> > 11 }
>
> What happends if your file
> matches /BEGIN/ but never
> matches /END/?
> You keep reading, even
> after you reach the end
> of the file.
>
>
>
> > 12 } else {
> > 13 print OUT "$_\n";
> > 14 }
> > 15 }
> > 16
> > 17 close IN;
> > 18 close OUT;
>
> > ----------------------------------
>
> > When I run this on a small file, about 28 kb, it works fine. When I
> > try it on a large file, about 3 gigs , I get the error:
>
> > "Read on closed filehandle <IN> at trim.pl line 6."
>
> > The file structure is essentailly the same, the only difference is the
> > size.
> > I bump into limits in many text editors when I try to access the large
> > file, and
> > I assume that this is a similar limitation in PERL. Is there any way
> > around this?
>
> > I also wouldn't mind some constructive criticism on the script
> > itself. It seems like there should
> > be a more elegant way to accomplish this task which might avoid this
> > problem.
> > Any suggestions?
>
> More concise:
>
> while (<IN>) {
> next if /BEGIN/ .. /END/;
> print OUT $_;
>
> }
>
> --
> Hope this helps,
> Steven
Steven,
Thanks for in suggestions. It works fine but I'm not familiar with the
use of '..' in the next statement.
I have used the 'next if' construct with single regular expressions,
is the above code just representing
the entire group of characters between the "BEGIN" and "END" strings?
If that's the case, and I don't have an "END", will the code print
everything between "BEGIN"
and the end of the file?
Thanks,
Russ D.
------------------------------
Date: 01 Feb 2007 17:02:48 -0600
From: jgraber@ti.com
Subject: Re: Read on closed filehandle
Message-Id: <yvnireltraf.fsf@famous02.dal.design.ti.com>
"Russ" <Russ.Dilley@gmail.com> writes:
> I have a very simple script to read an input file, skip everything
> between the strings "BEGIN" and "END", and store the results in an
> output file.
> I also wouldn't mind some constructive criticism on the script
> itself. It seems like there should
> be a more elegant way to accomplish this task which might avoid this
> problem.
> Any suggestions?
Size of input file is not the problem.
What did you want to happen if there is a BEGIN but no END?
# stop printing when see BEGIN, restart when see END
perl -n -e 'print unless /^BEGIN/ .. /^END/' < infile > outfile
--
Joel
------------------------------
Date: 01 Feb 2007 17:19:03 -0600
From: jgraber@ti.com
Subject: Re: Read on closed filehandle
Message-Id: <yvnejp9tqjc.fsf@famous02.dal.design.ti.com>
"Russ" <Russ.Dilley@gmail.com> writes:
> I have a very simple script to read an input file, skip everything
> between the strings "BEGIN" and "END", and store the results in an
> output file.
>
> 3 open(IN,"input.txt");
> 4 open(OUT,">output.txt");
> 5
> 6 while(<IN>) {
> 7 chomp;
> 8 if (/BEGIN/) {
> 9 until (/END/) {
> 10 $_ = <IN>;
> 11 }
> 12 } else {
> 13 print OUT "$_\n";
> 14 }
> 15 }
> 16
> 17 close IN;
> 18 close OUT;
> I also wouldn't mind some constructive criticism on the script
> itself.
additional comments to my previous reply
Be sure to use strict; and use warnings;
No use in chomping if you intend to add \n afterwards anyway.
What about anchoring BEGIN and END to front or end or whole line?
What about lines like "In every BEGINNING there is also an ENDING"?
Check for errors when opening and closing files,
since printing to a full disk wont be notied until the close.
close OUT || die "cant close '$outfile' because of error : $!\n";
Add the following lines to the end of your program
and run it as its own input file and then notice that it never exits.
# BEGIN
# eND
What did you want to do in the above case;
omit from BEGIN to eof,
or keep it?
--
Joel
------------------------------
Date: Thu, 1 Feb 2007 18:45:04 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Read on closed filehandle
Message-Id: <slrnes52cg.noj.tadmc@tadmc30.august.net>
Russ <Russ.Dilley@gmail.com> wrote:
> read an input file, skip everything
> between the strings "BEGIN" and "END",
^^^^^^^
> It seems like there should
> be a more elegant way to accomplish this task which might avoid this
> problem.
Your Question is Asked Frequently.
> Any suggestions?
perldoc -q between
How can I pull out lines between two patterns that are themselves on dif‐
ferent lines?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 01 Feb 2007 17:10:44 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Read on closed filehandle
Message-Id: <010220071710448749%jgibson@mail.arc.nasa.gov>
In article <1170376295.591550.133920@a75g2000cwd.googlegroups.com>,
Russ <Russ.Dilley@gmail.com> wrote:
> On Feb 1, 6:10 pm, "attn.steven....@gmail.com"
> <attn.steven....@gmail.com> wrote:
> > while (<IN>) {
> > next if /BEGIN/ .. /END/;
> > print OUT $_;
> >
> > }
>
> Thanks for in suggestions. It works fine but I'm not familiar with the
> use of '..' in the next statement.
> I have used the 'next if' construct with single regular expressions,
> is the above code just representing
> the entire group of characters between the "BEGIN" and "END" strings?
>
> If that's the case, and I don't have an "END", will the code print
> everything between "BEGIN"
> and the end of the file?
It is the range operator in scalar context, which acts like a
flip-flop. See 'perldoc perlop' and search for 'Range Operators'. The
operator will be false until /BEGIN/ is true, true until /END/ is true,
then false again until the next /BEGIN/, etc. If you do not have an
'END', the program will print from the final BEGIN to the end of the
file.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: Thu, 1 Feb 2007 19:14:26 -0600
From: "Dan Mercer" <damercer@comcast.net>
Subject: Re: Read on closed filehandle
Message-Id: <ZMGdnQaN-YbpDV_YnZ2dnUVZ_vOlnZ2d@comcast.com>
"Russ" <Russ.Dilley@gmail.com> wrote in message news:1170369248.849256.184780@m58g2000cwm.googlegroups.com...
: I have a very simple script to read an input file, skip everything
: between the strings "BEGIN" and "END", and store the results in an
: output
: file.
:
: ----------------------------
:
: 3 open(IN,"input.txt");
: 4 open(OUT,">output.txt");
: 5
: 6 while(<IN>) {
: 7 chomp;
: 8 if (/BEGIN/) {
: 9 until (/END/) {
: 10 $_ = <IN>;
You aren't checking to see if <IN> hits EOF. Undoubtedly,
the last line in the file contains the word "END"
Dan Mercer
: 11 }
: 12 } else {
: 13 print OUT "$_\n";
: 14 }
: 15 }
: 16
: 17 close IN;
: 18 close OUT;
:
: ----------------------------------
:
: When I run this on a small file, about 28 kb, it works fine. When I
: try it on a large file, about 3 gigs , I get the error:
:
: "Read on closed filehandle <IN> at trim.pl line 6."
:
: The file structure is essentailly the same, the only difference is the
: size.
: I bump into limits in many text editors when I try to access the large
: file, and
: I assume that this is a similar limitation in PERL. Is there any way
: around this?
:
: I also wouldn't mind some constructive criticism on the script
: itself. It seems like there should
: be a more elegant way to accomplish this task which might avoid this
: problem.
: Any suggestions?
:
: Thanks,
: Russ
:
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 94
*************************************