[31814] in Perl-Users-Digest
Perl-Users Digest, Issue: 3077 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 15 14:09:24 2010
Date: Sun, 15 Aug 2010 11:09:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 15 Aug 2010 Volume: 11 Number: 3077
Today's topics:
Re: Appropriate technique for altering a text file? <rvtol+usenet@xs4all.nl>
Re: Appropriate technique for altering a text file? <rvtol+usenet@xs4all.nl>
Re: Appropriate technique for altering a text file? <uri@StemSystems.com>
Re: Appropriate technique for altering a text file? <cartercc@gmail.com>
Re: Appropriate technique for altering a text file? <uri@StemSystems.com>
Re: Appropriate technique for altering a text file? <m@rtij.nl.invlalid>
Re: Appropriate technique for altering a text file? <jurgenex@hotmail.com>
Re: Appropriate technique for altering a text file? <hjp-usenet2@hjp.at>
Can't print entire line. <rodbass63@gmail.com>
Re: Can't print entire line. <jurgenex@hotmail.com>
Re: Can't print entire line. <tadmc@seesig.invalid>
Re: Can't print entire line. <hjp-usenet2@hjp.at>
Re: Can't print entire line. <tadmc@seesig.invalid>
Re: Can't print entire line. <hjp-usenet2@hjp.at>
Re: code snippet to convolve 2 vectors <rvtol+usenet@xs4all.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 15 Aug 2010 00:25:57 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <4c6717f5$0$22935$e4fe514c@news.xs4all.nl>
Uri Guttman wrote:
> cartercc:
> print OUT qq("$field1","$field2",..."$fieldn"\n);
>
> [...] why are you
> quoting scalars even in pseudo code? it ends with a newline outside a
> string too.
The qq() seems to be there to output the dquotes and the newline.
The ellipsis looks weird though.
--
Ruud
------------------------------
Date: Sun, 15 Aug 2010 00:28:13 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <4c67187d$0$22935$e4fe514c@news.xs4all.nl>
Martijn Lievaart wrote:
> Doing incremental reads under load will result in a lot of
> seeking so leads to a degradation of performance.
You underestimate your OS.
--
Ruud
------------------------------
Date: Sat, 14 Aug 2010 19:14:25 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <87mxsovlbi.fsf@quad.sysarch.com>
>>>>> "R" == Ruud <rvtol+usenet@xs4all.nl> writes:
R> Uri Guttman wrote:
>> cartercc:
>> print OUT qq("$field1","$field2",..."$fieldn"\n);
>>
>> [...] why are you
>> quoting scalars even in pseudo code? it ends with a newline outside a
>> string too.
R> The qq() seems to be there to output the dquotes and the newline.
R> The ellipsis looks weird though.
i didn't see the qq but it still looks odd. as i said it is more likely
psuedo code as there were no vars named $field1 and if there were, they
should be an array.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sat, 14 Aug 2010 19:15:19 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <0d18b356-d813-4a74-981b-9eb4d2412e81@g17g2000yqe.googlegroups.com>
On Aug 13, 11:34=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> also why are you
> quoting scalars even in pseudo code? it ends with a newline outside a
> string too.
Because I want my output file to look like this:
"George","Washington","1788"\n
"George","Washington","1792"\n
"John","Adams","1796"\n
etc.
> again it depends on the problem. try to parse a multiline structure line
> by line vs slurping. it is much easier to do a single regex on the whole
> file (in /g mode usually) and grab the structure then parse that. the
> line by line method needs state (possibly using the .. op which i have
> done plenty in this style), a variable to hold the stuff, a more complex
> loop, etc. slurp style is just so much cleaner.
AFAICR, this is the first time that I have parsed a file with a
multiline structure, and it was a personal rather than a business
purpose.
I see how one can say that the slurp style is cleaner, but as you say,
it depends on the job.
> i would make it a known and very useful when needed tool. it is not how
> you think but you just don't have experience seeing problems that are
> better slurped. many things work fine line by line but just as many work
> better slurping.
I had an experience several weeks ago. The short version is that I
solved a problem using Perl in less than an hour that some pretty high
powered folks spent two weeks trying to solve with SQL. They asked me
to help because I've had a lot of experience with SQL, and they just
couldn't get the query to work.
What I saw (and I admit that it took me a day to see it -- sometimes
I'm pretty dense) was that the problem wasn't a query problem but a
typical munging problem. The reason SQL didn't work was because the
problem was a screw and they were trying to use a hammer. When I used
a screw driver, it worked out very nicely.
The downside ;-) was that I had to confess that I used Perl instead of
a database, which caused some heartburn, because EVERYBODY KNOWS that
database technology (using Access) is far, far superior to the grungy
old technology that became obsolete during the last century.
I've been writing some Lisp, and sooner or later I'll solve a problem
with Lisp, and then maybe they'll be more accepting of Perl.
CC.
------------------------------
Date: Sat, 14 Aug 2010 23:28:52 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <87k4nstuyz.fsf@quad.sysarch.com>
>>>>> "c" == ccc31807 <cartercc@gmail.com> writes:
c> On Aug 13, 11:34 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
>> also why are you
>> quoting scalars even in pseudo code? it ends with a newline outside a
>> string too.
c> Because I want my output file to look like this:
c> "George","Washington","1788"\n
c> "George","Washington","1792"\n
c> "John","Adams","1796"\n
then use a CSV module to make sure you do it correctly. or a map join
print line like this:
print join( ',', map qq{"$_"}, @fields ), "\n" ;
that way you can use an array and not need single scalars named $field1
etc. anytime you see number suffixes on scalars, think array or think
the coder is a moron! :)
c> AFAICR, this is the first time that I have parsed a file with a
c> multiline structure, and it was a personal rather than a business
c> purpose.
then you haven't done much parsing so far. plenty of files have
multiline formats.
c> I've been writing some Lisp, and sooner or later I'll solve a
c> problem with Lisp, and then maybe they'll be more accepting of
c> Perl.
a strange way of getting perl accepted! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 15 Aug 2010 08:51:49 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <5dfkj7-slo.ln1@news.rtij.nl>
On Sun, 15 Aug 2010 00:28:13 +0200, Dr.Ruud wrote:
> Martijn Lievaart wrote:
>
>> Doing incremental reads under load will result in a lot of seeking so
>> leads to a degradation of performance.
>
> You underestimate your OS.
No, actually I don't. Obviously, a lot will be buffered, maybe a page,
maybe a cylinder worth of data. But once you read more than that, by
incremental reading you greatly increased the chance (we're talking about
a heavily loaded machine) that some other process needed the disk in the
mean time, leading to extra seeks.
In fact, many OSses are very good at optimizing seeks, so if you read
fast enough, the OS may recognize that the heads are in the right
position so avoid some seeks by giving your next read priority.
M4
------------------------------
Date: Sun, 15 Aug 2010 07:04:20 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <0hsf66d0rr746ik8v48m6qrfn8l8h2eba9@4ax.com>
Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>On Sun, 15 Aug 2010 00:28:13 +0200, Dr.Ruud wrote:
>
>> Martijn Lievaart wrote:
>>
>>> Doing incremental reads under load will result in a lot of seeking so
>>> leads to a degradation of performance.
>>
>> You underestimate your OS.
>
>No, actually I don't. Obviously, a lot will be buffered, maybe a page,
>maybe a cylinder worth of data. But once you read more than that, by
>incremental reading you greatly increased the chance (we're talking about
>a heavily loaded machine) that some other process needed the disk in the
>mean time, leading to extra seeks.
If your machines are so loaded that this becomes relevant then you
should have been looking for other storage solutions like RAID0 or SAN
or solid state disks for a while.
jue
------------------------------
Date: Sun, 15 Aug 2010 17:59:57 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Appropriate technique for altering a text file?
Message-Id: <slrni6g3nt.3q0.hjp-usenet2@hrunkner.hjp.at>
On 2010-08-15 03:28, Uri Guttman <uri@StemSystems.com> wrote:
>>>>>> "c" == ccc31807 <cartercc@gmail.com> writes:
> c> On Aug 13, 11:34 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >> also why are you
> >> quoting scalars even in pseudo code? it ends with a newline outside a
> >> string too.
>
> c> Because I want my output file to look like this:
> c> "George","Washington","1788"\n
> c> "George","Washington","1792"\n
> c> "John","Adams","1796"\n
>
> then use a CSV module to make sure you do it correctly. or a map join
> print line like this:
>
> print join( ',', map qq{"$_"}, @fields ), "\n" ;
I took $field1, $field2, etc. to be pseudocode. In reality the variables
were probably called $firstname, $lastname, $year, etc. (of course with
Carter you can never tell). I would probably have used something like
print join( ',', map quote($_), $firstname, $lastname, $year ), "\n" ;
to encapsulate any quoting that might be necessary (what happens when
one of the variables contains double quotes?). I'm partial about CSV
modules. There are so many different CSV formats, but each of them is
very simple: So you need a lot of time to figure out which CSV format is
needed, and if you have done so, it takes more time to figure out the
parameters for your CSV module then just coding it in plain Perl (I
still do use CSV_XS for speed - its simply much faster than any parser I
could write in Perl. But that's a secondary decision: First I write it
in Perl to get it correct. If it's too slow I use CSV_XS to get it fast).
hp
------------------------------
Date: Sat, 14 Aug 2010 19:02:27 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Can't print entire line.
Message-Id: <41613c84-0962-4eaf-9abf-be7807a58ad0@j18g2000yqd.googlegroups.com>
Hi,
Underneath __DATA__ is a snippet of a file. I can't print the entire
line, only the first word. You will see '^M', when the file was
produced it appended those '^M'. What am I doing wrong:
#!/usr/bin/perl -w
use strict;
my @tmsh_show = <DATA>;
while (my $line = <@tmsh_show>) {
chomp($line);
print "$line\n";
}
__DATA__
--------------------------------------^M
Status ^M
Availability : available^M
State : enabled^M
Reason : The pool is available^M
^M
Traffic ServerSide^M
Bits In 45.2M^M
Bits Out 0^M
Packets In 79.8K^M
Packets Out 0^M
Current Connections 0^M
Maximum Connections 35^M
Total Connections 8.2K^M
^M
Ltm::Pool Member: BatchESP_7438 172.16.70.66:7438^M
--------------------------------------------------^M
Status ^M
Availability : available^M
State : enabled^M
Reason : Pool member is available^M
^M
Traffic ServerSide General^M
Bits In 201.9K -^M
Bits Out 0 -^M
Packets In 342 -^M
Packets Out 0 -^M
Current Connections 0 -^M
Maximum Connections 3 -^M
Total Connections 41 -^M
Total Requests - 0^M
------------------------------
Date: Sat, 14 Aug 2010 20:20:26 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Can't print entire line.
Message-Id: <53ne661etqo9mamv8eoalncb7shgf9ubi5@4ax.com>
Nene <rodbass63@gmail.com> wrote:
>Underneath __DATA__ is a snippet of a file. I can't print the entire
>line, only the first word. You will see '^M', when the file was
>produced it appended those '^M'. What am I doing wrong:
You are processing the file on a different OS then where it was created.
In other words: you are running into the differences between line
endings in Unix, DOS/Windows, and Apple.
Run the file through unix2dos or dos2unix (whichever way you need to do
it) and those mysterious ^M should disappear.
jue
------------------------------
Date: Sat, 14 Aug 2010 22:22:22 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Can't print entire line.
Message-Id: <slrni6en4n.fus.tadmc@tadbox.sbcglobal.net>
Nene <rodbass63@gmail.com> wrote:
> What am I doing wrong:
It appears that you are trying to read twice from the same filehandle.
> #!/usr/bin/perl -w
Let me repeat what I wrote in a followup do you in December of 2008:
The modern, and much better, way is to use lexical warnings
instead of the command line switch:
#!/usr/bin/perl
use warnings;
The implication there was that you would adopt this better way.
Please do so.
> use strict;
>
> my @tmsh_show = <DATA>;
Every line has now been read into the array. The DATA file handle
is at end-of-file. You cannot read any more data from it.
> while (my $line = <@tmsh_show>) {
It looks like you are trying to read data again, but I cannot
figure out where you are expecting to read the data from...
Please tell us when you expect the condition to be false
so that the loop will exit.
> chomp($line);
>
> print "$line\n";
What is the point of removing a newline and then immediately adding a newline?
That's just plain silly.
> }
Either read them into an array and iterate over the array:
my @tmsh_show = <DATA>;
foreach (@tmsh_show) {
print;
}
or read them line by line:
while (<DATA>) {
print;
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sun, 15 Aug 2010 17:48:01 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Can't print entire line.
Message-Id: <slrni6g31i.3q0.hjp-usenet2@hrunkner.hjp.at>
On 2010-08-15 03:22, Tad McClellan <tadmc@seesig.invalid> wrote:
> Nene <rodbass63@gmail.com> wrote:
>> What am I doing wrong:
>
> It appears that you are trying to read twice from the same filehandle.
[...]
>
>> use strict;
>>
>> my @tmsh_show = <DATA>;
>
>
> Every line has now been read into the array. The DATA file handle
> is at end-of-file. You cannot read any more data from it.
>
>
>> while (my $line = <@tmsh_show>) {
>
>
> It looks like you are trying to read data again, but I cannot
> figure out where you are expecting to read the data from...
No, look again: He[1] tries to use @tmsh_show as a file handle. I haven't
the slightest idea what this is supposed to do, but what it appears to
do is split each element of @tmsh_show and return each field in turn.
With perl 5.10.1 program prints something like:
--------------------------------------^M
Status
^M
Availability
:
available^M
State
:
enabled^M
[...]
(which is not what the OP claims: It doesn't print the first word of
every line, it prints every word - but separately).
hp
[1] Or she, or whatever. I am always tempted to address people where I
can't guess whether they are male or female as "it", but that would be
impolite. The English language needs a gender-neutral personal pronoun,
like some Skandinavian languages have.
------------------------------
Date: Sun, 15 Aug 2010 11:12:18 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Can't print entire line.
Message-Id: <slrni6g487.hqk.tadmc@tadbox.sbcglobal.net>
Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
> On 2010-08-15 03:22, Tad McClellan <tadmc@seesig.invalid> wrote:
>> Nene <rodbass63@gmail.com> wrote:
>>> What am I doing wrong:
>>
>> It appears that you are trying to read twice from the same filehandle.
> [...]
>>
>>> use strict;
>>>
>>> my @tmsh_show = <DATA>;
>>
>>
>> Every line has now been read into the array. The DATA file handle
>> is at end-of-file. You cannot read any more data from it.
>>
>>
>>> while (my $line = <@tmsh_show>) {
>>
>>
>> It looks like you are trying to read data again, but I cannot
^^^^^^^^^^^^^^^^^^^^^^^^^
>> figure out where you are expecting to read the data from...
>
> No, look again: He[1] tries to use @tmsh_show as a file handle.
Using a filehandle _is_ trying to read data.
But the code isn't using @tmsh_show as a filehandle...
> I haven't
> the slightest idea what this is supposed to do,
Deparse says it is a glob():
while (defined((my $line = glob(join($", @tmsh_show))))) {
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sun, 15 Aug 2010 19:24:49 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Can't print entire line.
Message-Id: <slrni6g8n2.djr.hjp-usenet2@hrunkner.hjp.at>
On 2010-08-15 16:12, Tad McClellan <tadmc@seesig.invalid> wrote:
> Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
>> On 2010-08-15 03:22, Tad McClellan <tadmc@seesig.invalid> wrote:
>>> Nene <rodbass63@gmail.com> wrote:
>>>> What am I doing wrong:
>>>
>>> It appears that you are trying to read twice from the same filehandle.
>> [...]
>>>
>>>> use strict;
>>>>
>>>> my @tmsh_show = <DATA>;
>>>
>>>
>>> Every line has now been read into the array. The DATA file handle
>>> is at end-of-file. You cannot read any more data from it.
>>>
>>>
>>>> while (my $line = <@tmsh_show>) {
>>>
>>>
>>> It looks like you are trying to read data again, but I cannot
> ^^^^^^^^^^^^^^^^^^^^^^^^^
>>> figure out where you are expecting to read the data from...
>>
>> No, look again: He[1] tries to use @tmsh_show as a file handle.
>
>
> Using a filehandle _is_ trying to read data.
Sorry, I read that as "trying to read *the* data again", not reading
some other data which has not previously been read.
> But the code isn't using @tmsh_show as a filehandle...
>
>
>> I haven't
>> the slightest idea what this is supposed to do,
>
>
> Deparse says it is a glob():
>
> while (defined((my $line = glob(join($", @tmsh_show))))) {
>
Ah, yes. I forgot about the <> notation for glob. I never use that.
hp
------------------------------
Date: Sun, 15 Aug 2010 00:47:09 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: code snippet to convolve 2 vectors
Message-Id: <4c671ced$0$22903$e4fe514c@news.xs4all.nl>
Toralf Förster wrote:
> I'm looking for perl code examples to get the convoltion of 2 arrays eg.
> like this :
>
> [1, 2] x [3, 4] = [ 3, 10, 8]
You meant convolution?
Something like this?
( 1 + 2 * x ) * ( 3 + 4 * x ) =
3 + 6 * x + 4 * x + 8 * x^2 =
3 + 10 * x + 8 * x^2
#!/usr/bin/perl
use strict;
use warnings;
my @v = ( [ 1, 2 ], [ 3, 4 ] );
printf "[ %s, %s, %s ]\n",
$v[ 0 ][ 0 ] * $v[ 1 ][ 0 ],
$v[ 0 ][ 1 ] * $v[ 1 ][ 0 ] + $v[ 0 ][ 0 ] * $v[ 1 ][ 1 ],
$v[ 0 ][ 1 ] * $v[ 1 ][ 1 ];
__END__
--
Ruud
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 3077
***************************************