Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

basic hash question

4 views
Skip to first unread message

Dr Eberhard W Lisse

unread,
Jan 26, 2012, 6:41:59 PM1/26/12
to
Hi,

I have an anonymous hash with two elements and can print
them out like so.

foreach my $record (@$hash_records) {
print "$record->{A},$record->{B}\n";
}

However, I have several records of A with variable numbers of B (n=1 to
5) and would like

print one line of each A with B1, B2, B3, ..., Bn

can someone point me somewhere to read this up, or provide me with a
code fragment that does this?

greetings, el

--
If you want to email me, replace nospam with el

Jim Gibson

unread,
Jan 26, 2012, 7:21:03 PM1/26/12
to
In article <9oe6lr...@mid.individual.net>, Dr Eberhard W Lisse
<nos...@lisse.NA> wrote:

> Hi,
>
> I have an anonymous hash with two elements and can print
> them out like so.
>
> foreach my $record (@$hash_records) {

So $hash_records is a reference to an array.

> print "$record->{A},$record->{B}\n";

So the elements of the array are references to a hash, which always has
two elements with keys 'A' and 'B'.

> }
>
> However, I have several records of A with variable numbers of B (n=1 to
> 5) and would like

Do you mean that you have multiple (1 to 5) hashes wherein the value of
the element with key 'A' is the same while the value of the elements
with key 'B' vary, and this pattern repeats with multiple values of the
'A' elements?

>
> print one line of each A with B1, B2, B3, ..., Bn
>
> can someone point me somewhere to read this up, or provide me with a
> code fragment that does this?

Here are two ways:

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

my $hash_records = [
{ A => 1, B => 1 },
{ A => 3, B => 6 },
{ A => 2, B => 3 },
{ A => 3, B => 7 },
{ A => 3, B => 8 },
{ A => 2, B => 4 },
{ A => 1, B => 2 },
{ A => 3, B => 9 },
{ A => 2, B => 5 },
{ A => 3, B => 10 },
];

my $key = 0;
for my $record ( sort { $a->{A} <=> $b->{A} } @$hash_records ) {
my $new_key = $record->{A};
if( $new_key == $key ) {
print " $record->{B}";
}else{
print "\n$record->{A}: $record->{B}";
}
$key = $new_key;
}
print "\n\n";

my %output;
for my $record ( @$hash_records ) {
push( @{$output{$record->{A}}}, $record->{B} );
}
for my $key ( sort keys %output ) {
print "$key: ", join(', ', @{$output{$key}}), "\n";
}

Use 'cmp' and 'eq' instead of '<=>' and '==' if you are dealing with
text data instead of numerical data.

--
Jim Gibson

Ben Morrow

unread,
Jan 26, 2012, 7:27:06 PM1/26/12
to

Quoth nos...@lisse.NA:
>
> I have an anonymous hash with two elements and can print
> them out like so.
>
> foreach my $record (@$hash_records) {
> print "$record->{A},$record->{B}\n";
> }
>
> However, I have several records of A with variable numbers of B (n=1 to
> 5) and would like
>
> print one line of each A with B1, B2, B3, ..., Bn

It's not clear from this what your data structure looks like. Is it
something like

$hash_records = [
{ A => "foo", B1 => "bar", B2 => "baz" },
{ A => "blib", B1 => "blab", B3 => "blob" },
];

or something else?

If that's the right structure, then you want to reorganise it so it
looks like

$hash_records = [
{ A => "foo", B => ["bar", "baz"] },
{ A => "blib", B => ["blab", "blob"] },
];

since it will make manipulating it much easier. As a general rule of
thumb, when you start using names ending in numbers, you should probably
be using an array instead.

It would be best to do this reorganisztion at the point where you create
the data structure, since it better represents the structure of the
data, but if necessary you can use something like

for my $record (@$hash_records) {
my $A = $record->{A};
my @B = map $record->{$_},
sort
grep /^B/,
keys %$record;
print "$A, " . join ", ", @B;
}

to pull the keys you need out afterwards.

Ben

Tad McClellan

unread,
Jan 26, 2012, 11:20:29 PM1/26/12
to
Dr Eberhard W Lisse <nos...@lisse.NA> wrote:


> I have an anonymous hash with two elements


So, let's see the declaration of your anonymous hash with two elements.


> However, I have several records of A with variable numbers of B (n=1 to
> 5) and would like


Show us an example of this different data structure.

How to manipulate a data structure is entirely dependent on how
the data is structured.

Show us a data structure (a real one, not a pseudo ambiguous one) and
we will help you access it any way that you need.


--
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.

C.DeRykus

unread,
Jan 27, 2012, 2:36:55 AM1/27/12
to
On Jan 26, 3:41 pm, Dr Eberhard W Lisse <nos...@lisse.NA> wrote:

> ...
> can someone point me somewhere to read this up, or provide me with a
> code fragment that does this?
>

Perl data structures is a good tutorial
with lots of examples:

perldoc perldsc

--
Charles DeRykus

Dr Eberhard Lisse

unread,
Jan 27, 2012, 4:10:29 AM1/27/12
to
Thanks, will read.

el

on 2012-01-27 09:36 C.DeRykus said the following:

Dr Eberhard Lisse

unread,
Jan 27, 2012, 4:26:40 AM1/27/12
to
Sorry for being unclear,

foreach my $record (@$hash_records) {
print "$record->{A},$record->{B}\n";
}

results in:

A,a1
A,a2
B,b1
B,b2
B,b3
C,c1
C,c2
C,c3
C,c4
C,c5


but I need it to print out as something like

A,a1,a2,,,
B,b1,b2,b3,,
C,c1,c2,c3,c4,c5

Actually I use DNS::ZoneParse to parse a zone file with variable numbers
of name servers per domain.

greetings, el

on 2012-01-27 02:21 Jim Gibson said the following:
[...]
> Do you mean that you have multiple (1 to 5) hashes wherein the value of
> the element with key 'A' is the same while the value of the elements
> with key 'B' vary, and this pattern repeats with multiple values of the
> 'A' elements?

[...]

Dr Eberhard Lisse

unread,
Jan 27, 2012, 5:57:55 AM1/27/12
to
Jim,

This oes what I want, and I'll use the weekend to try and understand it
:-)-O

Thank you very much.

el


on 2012-01-27 02:21 Jim Gibson said the following:
[...]

Dr Eberhard Lisse

unread,
Jan 30, 2012, 3:18:57 AM1/30/12
to
Jim,

thank you very much, I think am getting the gist of it :-)-O

el

on 2012-01-27 12:57 Dr Eberhard Lisse said the following:

Jürgen Exner

unread,
Jan 30, 2012, 4:43:12 AM1/30/12
to
Dr Eberhard Lisse <nos...@lisse.NA> wrote:
>thank you very much, I think am getting the gist of it :-)-O

Whatever "it" is meant to be. "it" certainly is not Usenet manners.
Please do not top-post. It is confusing, irritating, and very much
frowned upon.

jue

Dr Eberhard Lisse

unread,
Jan 30, 2012, 6:00:45 AM1/30/12
to
Ok, next time I also full-quote.

el

on 2012-01-30 11:43 Jürgen Exner said the following:

Charlton Wilbur

unread,
Jan 30, 2012, 12:45:37 PM1/30/12
to
>>>>> "EL" == Eberhard Lisse <nos...@lisse.NA> writes:

EL> on 2012-01-30 11:43 Jürgen Exner said the following:

>> Whatever "it" is meant to be. "it" certainly is not Usenet
>> manners. Please do not top-post. It is confusing, irritating,
>> and very much frowned upon.

EL> Ok, next time I also full-quote. el

You just don't seem to get it.

You are here asking for help. It would behoove you to adhere to the
conventions of the forum in which you are asking for help. Not doing
so, and being such a pill about not doing so that the most knowledgeable
people get irritated enough to killfile you, means you are cutting
yourself off from the very help you seek.

And that "I can't, I'm using Emacs" excuse? Hey! So am I! And yet
somehow I manage not to top-post.

Charlton





--
Charlton Wilbur
cwi...@chromatico.net

Kaz Kylheku

unread,
Jan 30, 2012, 2:41:38 PM1/30/12
to
On 2012-01-30, Charlton Wilbur <cwi...@chromatico.net> wrote:
>>>>>> "EL" == Eberhard Lisse <nos...@lisse.NA> writes:
>
> EL> on 2012-01-30 11:43 Jürgen Exner said the following:
>
> >> Whatever "it" is meant to be. "it" certainly is not Usenet
> >> manners. Please do not top-post. It is confusing, irritating,
> >> and very much frowned upon.
>
> EL> Ok, next time I also full-quote. el
>
> You just don't seem to get it.

Oh he gets it, all right. He can get all of you to keep harping on this
triviality more predictably than robots 256 bytes of firmware.

Doc: 1, Wankers: 0.

Tad McClellan

unread,
Jan 30, 2012, 2:43:27 PM1/30/12
to
Charlton Wilbur <cwi...@chromatico.net> wrote:
>>>>>> "EL" == Eberhard Lisse <nos...@lisse.NA> writes:
>
> EL> on 2012-01-30 11:43 Jürgen Exner said the following:
>
> >> Whatever "it" is meant to be. "it" certainly is not Usenet
> >> manners. Please do not top-post. It is confusing, irritating,
> >> and very much frowned upon.
>
> EL> Ok, next time I also full-quote. el
>
> You just don't seem to get it.


I think he does get it, but that he does not care to be socially acceptable.

His attitude is "you all conform to my way" rather than he conforming
to the way of everyone else.

In other words, Eberhard Lisse is important, and everybody else is not.

Jon Du Kim

unread,
Jan 30, 2012, 4:07:08 PM1/30/12
to
You seem to have a firmer understanding of reality than the rest
of these dopes. "wankers" indeed!
Nice to see another half-way normal person post around here.

Kaz Kylheku

unread,
Jan 30, 2012, 4:47:34 PM1/30/12
to
Basically, you cannot brow-beat someone by the use of rude commands, into
having proper manners. That makes a farce out of the concept of manners.

In Japan, it is completely uncustomary to say something if someone
has done something rude (for which is there is plenty of opportunity to screw
up, in behavior, or speech).

One interesting difference is that here in North America, we say "excuse me" to
order to ask people to make space so that we can pass through a crowd, or sit.

By contrast, those kinds of accomodations usually happen without the exchange
of words. If you were to say the equivalent of excuse me, it would be rude,
because it says, "You who do not have the sense to make space for others, I'm
asking you to move."

Jürgen Exner

unread,
Jan 30, 2012, 8:21:17 PM1/30/12
to
Dr Eberhard Lisse <nos...@lisse.NA> wrote:
>Ok, next time I also full-quote.

***PLONK***

jue

Dr Eberhard W Lisse

unread,
Jan 30, 2012, 11:51:34 PM1/30/12
to
What Excuse?

Come on Feed me :-)-O

el

Dr Eberhard W Lisse

unread,
Jan 30, 2012, 11:53:19 PM1/30/12
to
You are welcome.

el

Kaz Kylheku

unread,
Jan 31, 2012, 1:23:14 AM1/31/12
to
On 2012-01-31, Jürgen Exner <jurg...@hotmail.com> wrote:
> Dr Eberhard Lisse <nos...@lisse.NA> wrote:
>>Ok, next time I also full-quote.
>
> ***PLONK***

Uh oh, Doc, now you've done it! Jürgen is using Forte Agent too,
so he might actually mean it.

(When you see a *plonk*, always check the headers. If they're using Google
Groups, or if their newsreader is an e-mail program, their killfile
is only mental, and within days, they will not be able to refrain from
following up to one of your posts.)

Dr Eberhard Lisse

unread,
Jan 31, 2012, 6:21:08 AM1/31/12
to
I think you have no clue whatsoever what is socially acceptable.
The arrogance displayed in several of your replies to (me and) others
asking questions here speaks for itself.

Which is why I suggested earlier you should go out more.

Or, you can speak to Isak N Jakobsen.

el


on 2012-01-30 21:43 Tad McClellan said the following:

Dr Eberhard Lisse

unread,
Jan 31, 2012, 6:27:55 AM1/31/12
to
Kaz,

you are right, this is what I have also observed in my 30 years of
residing in killfiles.

But then these Krauts are fairly predictable :-)-O and I couldn't care
less of checking whether they mean it or not.

el

on 2012-01-31 08:23 Kaz Kylheku said the following:
0 new messages