[33111] in Perl-Users-Digest
Perl-Users Digest, Issue: 4387 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 13 05:17:24 2015
Date: Fri, 13 Mar 2015 02:17:08 -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 Fri, 13 Mar 2015 Volume: 11 Number: 4387
Today's topics:
Read from huge text file <noreply2me@yahoo.com>
Re: Read from huge text file <peter@makholm.net>
Re: Setting hash values <justin.1502@purestblue.com>
Re: Setting hash values <peter@makholm.net>
Re: Setting hash values <bauhaus@futureapps.invalid>
Re: Setting hash values <whynot@pozharski.name>
Re: Setting hash values <rweikusat@mobileactivedefense.com>
Re: Setting hash values <justin.1502@purestblue.com>
Re: Setting hash values <rweikusat@mobileactivedefense.com>
Re: Setting hash values <bauhaus@futureapps.invalid>
Re: Setting hash values <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 13 Mar 2015 01:35:37 -0700
From: "Robert Crandal" <noreply2me@yahoo.com>
Subject: Read from huge text file
Message-Id: <7oidnRz7duPHPJ_InZ2dnUVZ5r6dnZ2d@giganews.com>
This is similar to an earlier problem, but with a few
differences.
Suppose I have the following input text file which is
divided into paragraphs. Each paragraph is separated
by a border of 25 star characters.:
[------------BEGIN INPUT (DELETE THIS)-----------]
playboys regrows correality requisition droits offered
angeles surfy wile lacrimation aged seignories practicing
hereinto workmanship fuggy municipally asdf underpinnings
brocket unpremeditated pinochle crazier coaeval obviously
able supinated hostler burrows artichoke vivant crosstown
********************
baneful celebrations angle growler landscape beside tzetzes
normal bootery bespoke henhouses tribuneship bouncer
displeasure crewman tenth curarization honestness sensitize
reminisces cometh fuk obscurantists eventualities mechanics
vanity crap nonalignment dowering nephew nonconfidence
********************
chaotically sooners rocketing luckiest holeproof damnableness
soc infertilely supernumerary expertise sulphid frisson
surceases joyously kins drooled agrarianism paraphrases ribby
wittiness grabbiest junketer accumulable hemokonia matriculants
sieged yuio forgoes *staking* nonadjacent offprint mug pawpaw
[------------END INPUT (DELETE THIS)-----------]
I need a loop that will read the entire file and save each paragraph
into it's own string variable or array or whatever. What is an
efficient way to do this?
Should I just read one line at time and append each sentence
to a string variable until I reach the next border of 25 star characters?
Or, is there a better way?
I hope my problem is clear. I plan to store each paragraph in a
string variable and do operations on that string. I can reuse the
same string variable once I'm done with each operation.
~Rob
------------------------------
Date: Fri, 13 Mar 2015 10:04:55 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: Read from huge text file
Message-Id: <87y4n1b748.fsf@vps1.hacking.dk>
"Robert Crandal" <noreply2me@yahoo.com> writes:
> Should I just read one line at time and append each sentence
> to a string variable until I reach the next border of 25 star characters?
That would be my solution.
Unless your real problem is something quite simple as just counting the
paragraphs I wouldn't this part will be a bottleneck worth optimizing
unless benchmarks clearly shows otherwise.
//Makhonlm
------------------------------
Date: Tue, 10 Mar 2015 16:19:42 +0000
From: Justin C <justin.1502@purestblue.com>
Subject: Re: Setting hash values
Message-Id: <u5i3tb-eqb.ln1@zem.masonsmusic.co.uk>
On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
> I have a hash that holds some status information. I need to clear/set
> all keys to the same value. Is there a better way than:
>
> foreach (keys %hash)
> {
> $hash{$_} = 1;
> }
This is shorter and quite clear:
%hash = map {$_ => 1} keys %hash;
You could just turn around what you've got:
$hash{$_} = 1 for keys %hash
I think the map version more explicitly shows its intention.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 11 Mar 2015 11:18:48 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: Setting hash values
Message-Id: <8761a7et13.fsf@vps1.hacking.dk>
Peter Makholm <peter@makholm.net> writes:
> @hash{ keys %hash } = (1) x keys %hash;
I agree with Peter J. Holzer's comments.
BTW, I don't mind the use of hash slices, even though less experinced perl
programmers might require a bit more time reading the code. I am a bit
more worried about the x operator and the risk that a less experienced
developer might decide that the parenthesis is unneeded.
//Makholm
------------------------------
Date: Wed, 11 Mar 2015 11:24:34 +0100
From: "G.B." <bauhaus@futureapps.invalid>
Subject: Re: Setting hash values
Message-Id: <mdp53k$oqt$1@dont-email.me>
On 10.03.15 17:19, Justin C wrote:
> On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
>> I have a hash that holds some status information. I need to clear/set
>> all keys to the same value. Is there a better way than:
>>
>> foreach (keys %hash)
>> {
>> $hash{$_} = 1;
>> }
>
> This is shorter and quite clear:
I agree it is shorter, in particular when counting
both characters and lines,
> %hash = map {$_ => 1} keys %hash;
but why is it quite clear? That seems to be subjective
if stated without reason. And is "map" clearly expressing
what was intended?
TTBOMK, functional map is usually considered a step up
from foreach. Technically, it also incurs lists and how
they correspond with Perls hashes, foreach does not.
(And I like map.)
I also think the one assignment might not be what was
intended, as %hash = map ... overwrites the previous
object with a new one created by map. What if %hash
is tied? Should CLEAR be called?
------------------------------
Date: Wed, 11 Mar 2015 09:55:48 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Setting hash values
Message-Id: <slrnmfvt84.k38.whynot@orphan.zombinet>
with <mdnerm$19va$1@news.ntua.gr> George Mpouras wrote:
> On 10/3/2015 5:54 μμ, Dave Saville wrote:
>> I have a hash that holds some status information. I need to clear/set
>> all keys to the same value. Is there a better way than:
>>
>> foreach (keys %hash) { $hash{$_} = 1; }
*SKIP*
> # b method
> %hash = map {++$i % 2 ? ($_,'_+_'):()} %hash{keys %hash};
It's not about strict/warnings anymore. It, literally, doesn't compile.
You've intended to make this:
%hash = map { ++$i % 2 ? ( $_, '_+_' ) : ( ) } %hash;
What rises a question:
What would be most obscure way to say what Peter said?
p.s. It was (what Peter said):
$_ = 1 foreach values %hash;
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
------------------------------
Date: Wed, 11 Mar 2015 10:57:17 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Setting hash values
Message-Id: <87a8zjhkdu.fsf@doppelsaurus.mobileactivedefense.com>
Peter Makholm <peter@makholm.net> writes:
> Peter Makholm <peter@makholm.net> writes:
>
>> @hash{ keys %hash } = (1) x keys %hash;
>
> I agree with Peter J. Holzer's comments.
As I already pointed out: It can't be very obscure when it suggested
itself as obvious solution (or least something worth of giving it a try)
to three people independently. As 'use of a hash slice', I'd rate this
as 'curiosity worth knowing about': Having the name of the hash in three
different places makes this 'high maintenance' code because the name
would need to be changed in three different places in order to apply the
construct to a different hash, although that's rather a theoretical
concern for a single line of code.
> BTW, I don't mind the use of hash slices, even though less experinced perl
> programmers might require a bit more time reading the code. I am a bit
> more worried about the x operator and the risk that a less experienced
> developer might decide that the parenthesis is unneeded.
'Less experienced developers' (as opposed to 'inexperienced CS students
with superiority complex based on that') never change code they didn't
write ...
------------------------------
Date: Wed, 11 Mar 2015 12:00:51 +0000
From: Justin C <justin.1502@purestblue.com>
Subject: Re: Setting hash values
Message-Id: <jcn5tb-vjd.ln1@zem.masonsmusic.co.uk>
On 2015-03-11, G.B. <bauhaus@futureapps.invalid> wrote:
> On 10.03.15 17:19, Justin C wrote:
>> On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
>>> I have a hash that holds some status information. I need to clear/set
>>> all keys to the same value. Is there a better way than:
>>>
>>> foreach (keys %hash)
>>> {
>>> $hash{$_} = 1;
>>> }
>>
>> This is shorter and quite clear:
>
> I agree it is shorter, in particular when counting
> both characters and lines,
>
>> %hash = map {$_ => 1} keys %hash;
>
> but why is it quite clear? That seems to be subjective
> if stated without reason. And is "map" clearly expressing
> what was intended?
You're right, it is subjective. It is more clear to *me*.
> I also think the one assignment might not be what was
> intended, as %hash = map ... overwrites the previous
> object with a new one created by map. What if %hash
> is tied? Should CLEAR be called?
I have no experience with tied hashes and so never considered them
in my reply. As is always the case, TIMTOWTDI, and YMMV.
Justin.
--
Justin C, by the sea.
------------------------------
Date: Wed, 11 Mar 2015 16:08:58 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Setting hash values
Message-Id: <87r3sv1pph.fsf@doppelsaurus.mobileactivedefense.com>
"G.B." <bauhaus@futureapps.invalid> writes:
> On 10.03.15 17:19, Justin C wrote:
>> On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
[...]
>> %hash = map {$_ => 1} keys %hash;
[...]
> I also think the one assignment might not be what was
> intended, as %hash = map ... overwrites the previous
> object with a new one created by map.
It doesn't. map returns a list of pairs and the pairs on this list
replace whatever content %hash presently had.
------------------------------
Date: Wed, 11 Mar 2015 18:06:18 +0100
From: "G.B." <bauhaus@futureapps.invalid>
Subject: Re: Setting hash values
Message-Id: <mdpskt$q6e$1@dont-email.me>
On 11.03.15 17:08, Rainer Weikusat wrote:
> "G.B." <bauhaus@futureapps.invalid> writes:
>> On 10.03.15 17:19, Justin C wrote:
>>> On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
>
> [...]
>
>>> %hash = map {$_ => 1} keys %hash;
>
> [...]
>
>> I also think the one assignment might not be what was
>> intended, as %hash = map ... overwrites the previous
>> object with a new one created by map.
>
> It doesn't. map returns a list of pairs and the pairs on this list
> replace whatever content %hash presently had.
Nomenclature? Or is it working in this particular case,
assuming a never changing set of keys? To wit,
use Devel::Peek;
%hash = (1,2,3,4); Dump \%hash;
%hash = ();
%hush = (5,6,7,8);
%hash = (1,2,3,4); Dump \%hash;
(I have also seen some changes of positions in SVs varying
the above.)
------------------------------
Date: Wed, 11 Mar 2015 17:56:15 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Setting hash values
Message-Id: <87bnjz1kqo.fsf@doppelsaurus.mobileactivedefense.com>
"G.B." <bauhaus@futureapps.invalid> writes:
> On 11.03.15 17:08, Rainer Weikusat wrote:
>> "G.B." <bauhaus@futureapps.invalid> writes:
>>> On 10.03.15 17:19, Justin C wrote:
>>>> On 2015-03-10, Dave Saville <No-Spam@deezee.org> wrote:
>>
>> [...]
>>
>>>> %hash = map {$_ => 1} keys %hash;
>>
>> [...]
>>
>>> I also think the one assignment might not be what was
>>> intended, as %hash = map ... overwrites the previous
>>> object with a new one created by map.
>>
>> It doesn't. map returns a list of pairs and the pairs on this list
>> replace whatever content %hash presently had.
>
> Nomenclature? Or is it working in this particular case,
> assuming a never changing set of keys? To wit,
>
> use Devel::Peek;
>
> %hash = (1,2,3,4); Dump \%hash;
> %hash = ();
> %hush = (5,6,7,8);
> %hash = (1,2,3,4); Dump \%hash;
>
> (I have also seen some changes of positions in SVs varying
> the above.)
When you look at the output, you'll notice that the same HV is dumped in
both cases: At the level of the language, %hash is a hash object, not
something referring to a hash. The hash-slot of the glob *hash refers to
the hash object and it's possible to make it refer to a different hash
object but only by assigning one to it, not by assigning a list to the
hash:
perl -MDevel::Peek -e '%h = qw(a b c d); Dump(\%h); %h = qw(e f g h); Dump(\%h); *h = { i, j, k, l}; Dump(\%h)'
------------------------------
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 4387
***************************************