Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > basic hash question

Reply
Thread Tools

basic hash question

 
 
Dr Eberhard W Lisse
Guest
Posts: n/a
 
      01-26-2012
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      01-27-2012
In article <(E-Mail Removed)>, Dr Eberhard W Lisse
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      01-27-2012
On Jan 26, 3:41*pm, Dr Eberhard W Lisse <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr Eberhard Lisse
Guest
Posts: n/a
 
      01-27-2012
Thanks, will read.

el

on 2012-01-27 09:36 C.DeRykus said the following:
> On Jan 26, 3:41 pm, Dr Eberhard W Lisse <(E-Mail Removed)> 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


 
Reply With Quote
 
Dr Eberhard Lisse
Guest
Posts: n/a
 
      01-27-2012
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?


[...]
 
Reply With Quote
 
Dr Eberhard Lisse
Guest
Posts: n/a
 
      01-27-2012
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:
[...]
> 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.
>


 
Reply With Quote
 
Dr Eberhard Lisse
Guest
Posts: n/a
 
      01-30-2012
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:
> Jim,
>
> This oes what I want, and I'll use the weekend to try and
> understand it -O
>
> Thank you very much.
>
> el

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-30-2012
Dr Eberhard Lisse <(E-Mail Removed)> 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

>on 2012-01-27 12:57 Dr Eberhard Lisse said the following:
>> Jim,
>>
>> This oes what I want, and I'll use the weekend to try and
>> understand it -O
>>
>> Thank you very much.
>>
>> el

 
Reply With Quote
 
Dr Eberhard Lisse
Guest
Posts: n/a
 
      01-30-2012
Ok, next time I also full-quote.

el

on 2012-01-30 11:43 Jürgen Exner said the following:
> Dr Eberhard Lisse <(E-Mail Removed)> 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
>
>> on 2012-01-27 12:57 Dr Eberhard Lisse said the following:
>>> Jim,
>>>
>>> This oes what I want, and I'll use the weekend to try and
>>> understand it -O
>>>
>>> Thank you very much.
>>>
>>> el


 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      01-30-2012
>>>>> "EL" == Eberhard Lisse <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET Jaime MCSD 2 09-20-2003 05:16 AM



Advertisments