Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

In specifications, Murphy's Law supersedes Ohm's.


devel / comp.lang.perl.misc / Building hash of hashes?

SubjectAuthor
* Building hash of hashes?Otto J. Makela
+* Re: Building hash of hashes?AnonymousCoward
|`* Re: Building hash of hashes?Otto J. Makela
| `- Re: Building hash of hashes?Randal L. Schwartz
`* Re: Building hash of hashes?George Bouras
 `* Re: Building hash of hashes?Otto J. Makela
  `- Re: Building hash of hashes?George Bouras

1
Building hash of hashes?

<87y28nyomr.fsf@tigger.extechop.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=157&group=comp.lang.perl.misc#157

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: om@iki.fi (Otto J. Makela)
Newsgroups: comp.lang.perl.misc
Subject: Building hash of hashes?
Date: Fri, 27 Aug 2021 16:59:40 +0300
Organization: Games and Theory
Lines: 52
Message-ID: <87y28nyomr.fsf@tigger.extechop.net>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b86f33f82814270f6e2b10bd0e201b49";
logging-data="20094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WeGmyb6kKqFiG2ddcttE2"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:lqw75fx+ddTli1WZhOg8qG5utHE=
sha1:7qUuFO/h+drOYp3vakTwrCibdjw=
X-Face: 'g'S,X"!c;\pfvl4ljdcm?cDdk<-Z;`x5;YJPI-cs~D%;_<\V3!3GCims?a*;~u$<FYl@"E
c?3?_J+Zwn~{$8<iEy}EqIn_08"`oWuqO$#(5y3hGq8}BG#sag{BL)u8(c^Lu;*{8+'Z-k\?k09ILS
X-URL: http://www.iki.fi/om/
Mail-Copies-To: never
 by: Otto J. Makela - Fri, 27 Aug 2021 13:59 UTC

The following demo builds a counter (using a hash of hashes) into
memory from the DATA section and then displays the result. However,
I'm not terribly fond of the way I create the hashes in the hash.

This demo of course is a simplification of my actual use case,
where I ended up having three-layered hashes embedded with
complex initialization.

Does anyone have suggestions for a "neater" way to do this?

----

#!/usr/bin/perl
use strict;
use warnings;

# Collect DATA into hash of hashes
my %data;
while(<DATA>) {
chomp;
(my $a, my $b) = split(/\s+/,$_,2);
if(defined $data{$a}) {
$data{$a}{$b}++;
} else {
$data{$a} = { $b => 1 };
}
}

# Print out collected data
foreach my $a (sort keys %data) {
print $a;
foreach my $b (sort keys $data{$a}) {
print "\t",$b,"\t",$data{$a}{$b},"\n";
}
}

__DATA__
yy cc
xx aa
yy aa
yy bb
xx cc
zz cc
yy bb
xx bb
yy aa

--
/* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
/* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
/* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
/* * * Computers Rule 01001111 01001011 * * * * * * */

Re: Building hash of hashes?

<sgbk6i$fqd$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=164&group=comp.lang.perl.misc#164

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anonymous@coward.com (AnonymousCoward)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
Date: Fri, 27 Aug 2021 21:07:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <sgbk6i$fqd$1@dont-email.me>
References: <87y28nyomr.fsf@tigger.extechop.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 27 Aug 2021 21:07:30 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="90e30ebe98b46cf4badb8269c1476910";
logging-data="16205"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183apSoKgAaHGxRgl3uBjl4l4ajabEtBoE="
User-Agent: Pan/0.146 (Hic habitat felicitas; 476b5a6 /data/tmp/pan-git/pan)
Cancel-Lock: sha1:WS76FF1f0uacK5/GMEhcHeReZIg=
 by: AnonymousCoward - Fri, 27 Aug 2021 21:07 UTC

How about:

#!/usr/bin/perl
use strict;
use warnings;

# Collect DATA into hash of hashes
my %data;
while(my $l = <DATA>) {
chomp $l;
(my $a, my $b) = split(/\s+/,$l,2);
$data{$a}{$b}++;
}

# Print out collected data
while (my ($a, $d) = each %data) {
print $a;
print "\t$_\t@{[$d->{$_}]}\n" for (sort keys %$d);
}

__DATA__
yy cc
xx aa
yy aa
yy bb
xx cc
zz cc
yy bb
xx bb
yy aa

Re: Building hash of hashes?

<87r1eb2jjo.fsf@tigger.extechop.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=170&group=comp.lang.perl.misc#170

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: om@iki.fi (Otto J. Makela)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
Date: Mon, 30 Aug 2021 15:39:39 +0300
Organization: Games and Theory
Lines: 18
Message-ID: <87r1eb2jjo.fsf@tigger.extechop.net>
References: <87y28nyomr.fsf@tigger.extechop.net> <sgbk6i$fqd$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="1df77546c10323f7876bd19cd8a303ef";
logging-data="31307"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+7NY68rPfwihnpuC0Q8qB"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:60YjoE3U6BR7BjSy3ZpX8hyvMvs=
sha1:vNp+/5uWWsu5K8YMEFKyPN+A4Eg=
X-Face: 'g'S,X"!c;\pfvl4ljdcm?cDdk<-Z;`x5;YJPI-cs~D%;_<\V3!3GCims?a*;~u$<FYl@"E
c?3?_J+Zwn~{$8<iEy}EqIn_08"`oWuqO$#(5y3hGq8}BG#sag{BL)u8(c^Lu;*{8+'Z-k\?k09ILS
X-URL: http://www.iki.fi/om/
Mail-Copies-To: never
 by: Otto J. Makela - Mon, 30 Aug 2021 12:39 UTC

AnonymousCoward <anonymous@coward.com> wrote:

> How about:
....
> (my $a, my $b) = split(/\s+/,$l,2);
> $data{$a}{$b}++;

Okay, this was actually what I felt was the ugliest bit:
I wasn't aware that anonymous hashes would automatically
get instantiated into the hash. Thus the convolutions I
went to manually build them were quite unnecessary.

I've learnt a new thing here, thank you!
--
/* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
/* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
/* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
/* * * Computers Rule 01001111 01001011 * * * * * * */

Re: Building hash of hashes?

<sgnd13$106t$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=171&group=comp.lang.perl.misc#171

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!aioe.org!XeJnFtFGyEwmBNcUtc7jDw.user.46.165.242.75.POSTED!not-for-mail
From: foo@example.com (George Bouras)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
Date: Wed, 1 Sep 2021 11:18:43 +0300
Organization: Aioe.org NNTP Server
Message-ID: <sgnd13$106t$1@gioia.aioe.org>
References: <87y28nyomr.fsf@tigger.extechop.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="32989"; posting-host="XeJnFtFGyEwmBNcUtc7jDw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: el
X-Notice: Filtered by postfilter v. 0.9.2
 by: George Bouras - Wed, 1 Sep 2021 08:18 UTC

#!/bin/perl
use strict;
use warnings;

# Collect DATA into hash of hashes
my %data;
while (<DATA>) {
$data{$1}->{$2}++ if /^(\S+)\s+(.*?)\s*$/
}

use Data::Dumper; print Dumper \%data;

__DATA__
yy cc
xx aa
yy aa
yy bb
xx cc
zz cc
yy bb
xx bb
yy aa

Re: Building hash of hashes?

<86fsumo4ry.fsf@red.stonehenge.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=174&group=comp.lang.perl.misc#174

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!4.us.feeder.erje.net!2.eu.feeder.erje.net!feeder.erje.net!news.uzoreto.com!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx03.iad.POSTED!not-for-mail
From: merlyn@stonehenge.com (Randal L. Schwartz)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
References: <87y28nyomr.fsf@tigger.extechop.net> <sgbk6i$fqd$1@dont-email.me>
<87r1eb2jjo.fsf@tigger.extechop.net>
x-mayan-date: Long count = 13.0.8.14.17; tzolkin = 9 Caban; haab = 15 Mol
Message-ID: <86fsumo4ry.fsf@red.stonehenge.com>
Organization: Stonehenge Consulting Services; Portland, Oregon, USA
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (berkeley-unix)
Cancel-Lock: sha1:vZGYXlzskeMgaEpM2Tng2+l9MOM=
MIME-Version: 1.0
Content-Type: text/plain
Lines: 14
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Fri, 03 Sep 2021 01:03:06 UTC
Date: Thu, 02 Sep 2021 17:56:49 -0700
X-Received-Bytes: 1528
 by: Randal L. Schwartz - Fri, 3 Sep 2021 00:56 UTC

>>>>> "Otto" == Otto J Makela <om@iki.fi> writes:

Otto> Okay, this was actually what I felt was the ugliest bit:
Otto> I wasn't aware that anonymous hashes would automatically
Otto> get instantiated into the hash. Thus the convolutions I
Otto> went to manually build them were quite unnecessary.

Yes, thanks to the loved/hated feature of autovivification.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Dart/Flutter consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig

Re: Building hash of hashes?

<87o895ztyu.fsf@tigger.extechop.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=175&group=comp.lang.perl.misc#175

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: om@iki.fi (Otto J. Makela)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
Date: Mon, 06 Sep 2021 16:58:33 +0300
Organization: Games and Theory
Lines: 13
Message-ID: <87o895ztyu.fsf@tigger.extechop.net>
References: <87y28nyomr.fsf@tigger.extechop.net>
<sgnd13$106t$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="5993a7736f22e5f5c20cc9a5c87bdfad";
logging-data="11066"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pBwc9B0aObn9mMV84mpCc"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:eCDsLE9ZmWN/s0bKy4tYjyyjf1o=
sha1:ZYQTDJpdgussVrJ7VkREevgRhQQ=
X-Face: 'g'S,X"!c;\pfvl4ljdcm?cDdk<-Z;`x5;YJPI-cs~D%;_<\V3!3GCims?a*;~u$<FYl@"E
c?3?_J+Zwn~{$8<iEy}EqIn_08"`oWuqO$#(5y3hGq8}BG#sag{BL)u8(c^Lu;*{8+'Z-k\?k09ILS
X-URL: http://www.iki.fi/om/
Mail-Copies-To: never
 by: Otto J. Makela - Mon, 6 Sep 2021 13:58 UTC

George Bouras <foo@example.com> wrote:

> $data{$1}->{$2}++ if /^(\S+)\s+(.*?)\s*$/

This works just as well even without the explicitly written ->

My actual use case was significantly more convoluted,
with three levels of indirection, this was just a simplification.
--
/* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
/* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
/* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
/* * * Computers Rule 01001111 01001011 * * * * * * */

Re: Building hash of hashes?

<sh5up3$11oa$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=176&group=comp.lang.perl.misc#176

  copy link   Newsgroups: comp.lang.perl.misc
Path: i2pn2.org!i2pn.org!aioe.org!XeJnFtFGyEwmBNcUtc7jDw.user.46.165.242.75.POSTED!not-for-mail
From: foo@example.com (George Bouras)
Newsgroups: comp.lang.perl.misc
Subject: Re: Building hash of hashes?
Date: Mon, 6 Sep 2021 23:47:30 +0300
Organization: Aioe.org NNTP Server
Message-ID: <sh5up3$11oa$1@gioia.aioe.org>
References: <87y28nyomr.fsf@tigger.extechop.net>
<sgnd13$106t$1@gioia.aioe.org> <87o895ztyu.fsf@tigger.extechop.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="34570"; posting-host="XeJnFtFGyEwmBNcUtc7jDw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: el
 by: George Bouras - Mon, 6 Sep 2021 20:47 UTC

> My actual use case was significantly more convoluted,
> with three levels of indirection, this was just a simplification.
>
no problem if you want more help


devel / comp.lang.perl.misc / Building hash of hashes?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor