basic hash question

Discussion in 'Perl Misc' started by Dr Eberhard W Lisse, Jan 26, 2012.

  1. 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
     
    Dr Eberhard W Lisse, Jan 26, 2012
    #1
    1. Advertising

  2. Dr Eberhard W Lisse

    Jim Gibson Guest

    In article <>, Dr Eberhard W Lisse
    <> 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
     
    Jim Gibson, Jan 27, 2012
    #2
    1. Advertising

  3. Dr Eberhard W Lisse

    C.DeRykus Guest

    On Jan 26, 3:41 pm, Dr Eberhard W Lisse <> 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
     
    C.DeRykus, Jan 27, 2012
    #3
  4. 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 <> 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, Jan 27, 2012
    #4
  5. 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, Jan 27, 2012
    #5
  6. 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.
    >
     
    Dr Eberhard Lisse, Jan 27, 2012
    #6
  7. 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
     
    Dr Eberhard Lisse, Jan 30, 2012
    #7
  8. Dr Eberhard Lisse <> 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
     
    Jürgen Exner, Jan 30, 2012
    #8
  9. Ok, next time I also full-quote.

    el

    on 2012-01-30 11:43 Jürgen Exner said the following:
    > Dr Eberhard Lisse <> 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
     
    Dr Eberhard Lisse, Jan 30, 2012
    #9
  10. >>>>> "EL" == Eberhard Lisse <> 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
     
    Charlton Wilbur, Jan 30, 2012
    #10
  11. Dr Eberhard W Lisse

    Kaz Kylheku Guest

    On 2012-01-30, Charlton Wilbur <> wrote:
    >>>>>> "EL" == Eberhard Lisse <> 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.
     
    Kaz Kylheku, Jan 30, 2012
    #11
  12. Dr Eberhard W Lisse

    Jon Du Kim Guest

    On 1/30/12 2:41 PM, Kaz Kylheku wrote:
    > On 2012-01-30, Charlton Wilbur<> wrote:
    >>>>>>> "EL" == Eberhard Lisse<> 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.


    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.
     
    Jon Du Kim, Jan 30, 2012
    #12
  13. Dr Eberhard W Lisse

    Kaz Kylheku Guest

    On 2012-01-30, Jon Du Kim <> wrote:
    > On 1/30/12 2:41 PM, Kaz Kylheku wrote:
    >> On 2012-01-30, Charlton Wilbur<> wrote:
    >> Doc: 1, Wankers: 0.

    >
    > 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."
     
    Kaz Kylheku, Jan 30, 2012
    #13
  14. Dr Eberhard Lisse <> wrote:
    >Ok, next time I also full-quote.


    ***PLONK***

    jue

    >on 2012-01-30 11:43 Jürgen Exner said the following:
    >> Dr Eberhard Lisse <> 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
     
    Jürgen Exner, Jan 31, 2012
    #14
  15. What Excuse?

    Come on Feed me :)-O

    el


    On 2012-01-30 19:45 , Charlton Wilbur wrote:
    >>>>>> "EL" == Eberhard Lisse <> 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
    >
    >
    >
    >
    >



    --
    If you want to email me, replace nospam with el
     
    Dr Eberhard W Lisse, Jan 31, 2012
    #15
  16. You are welcome.

    el

    On 2012-01-30 23:07 , Jon Du Kim wrote:
    > On 1/30/12 2:41 PM, Kaz Kylheku wrote:
    >> On 2012-01-30, Charlton Wilbur<> wrote:
    >>>>>>>> "EL" == Eberhard Lisse<> 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.

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



    --
    If you want to email me, replace nospam with el
     
    Dr Eberhard W Lisse, Jan 31, 2012
    #16
  17. Dr Eberhard W Lisse

    Kaz Kylheku Guest

    On 2012-01-31, Jürgen Exner <> wrote:
    > Dr Eberhard Lisse <> 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.)
     
    Kaz Kylheku, Jan 31, 2012
    #17
  18. 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:
    > Charlton Wilbur <> wrote:
    >>>>>>> "EL" == Eberhard Lisse <> 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.
    >
    >
     
    Dr Eberhard Lisse, Jan 31, 2012
    #18
  19. Re: basic hash question [OT}

    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:
    > On 2012-01-31, Jürgen Exner <> wrote:
    >> Dr Eberhard Lisse <> 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, Jan 31, 2012
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Engineer
    Replies:
    6
    Views:
    673
    Jeremy Bowers
    May 1, 2005
  2. rp
    Replies:
    1
    Views:
    583
    red floyd
    Nov 10, 2011
  3. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    172
    Ronald Fischer
    Aug 23, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    670
    David A. Black
    Jul 2, 2008
  5. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    686
    Älphä Blüë
    Jul 18, 2009
Loading...

Share This Page