basic hash question

  • Thread starter Dr Eberhard W Lisse
  • Start date
D

Dr Eberhard W Lisse

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
 
J

Jim Gibson

Dr Eberhard W Lisse said:
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.
 
C

C.DeRykus

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

Dr Eberhard Lisse

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?

[...]
 
D

Dr Eberhard Lisse

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:
[...]
 
J

Jürgen Exner

Dr Eberhard Lisse said:
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
 
D

Dr Eberhard Lisse

Ok, next time I also full-quote.

el

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
 
C

Charlton Wilbur

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

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
 
K

Kaz Kylheku

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


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

Jon Du Kim

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.

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

Kaz Kylheku

You seem to have a firmer understanding of reality than the rest
of these dopes. "wankers" indeed!

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

Dr Eberhard W Lisse

What Excuse?

Come on Feed me :)-O

el


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


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
 
D

Dr Eberhard W Lisse

You are welcome.

el

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

Kaz Kylheku

***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.)
 
D

Dr Eberhard Lisse

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
 
D

Dr Eberhard Lisse

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top