in reply to References for subroutines to Hashes in Hashes :)

Here is some code that does what you want... I think...
my %big_ash; my @keys; while(<DATA>) { my @fields = split /--+/, $_; next unless @fields; if($. == 1) { #first line @keys = @fields; next; } my %nr; @nr{@keys} = @fields; $big_ash{$nr{NR}} = \%nr; } print "$big_ash{14}{Dates}\n"; __DATA__ NR------E-mail----------Comment---------Dates-------------Age--Action_ +date 12------Tom@info.com----blablabla-------AM0,PM0,AM1,PM1---32---10-10-2 +001 13------Frank@info.com--blablabla-------AM0,PM0-----------45---10-10-2 +001 14------Sand@info.com---blablabla-------AM1,PM1,PM2-------47---10-10-2 +001 14------Sand@info.com---blablabla-------AM1,PM1,PM2-------47---12-10-2 +001
this assumes you always have at least -- as a record separator... you may want to use spaces to pad your data and look into unpack to get your data back... or change your delimiter to something like | that you don't have in any of your data...
Since a hash within a hash is stored as a reference, you do not need the \$analyst_record{$c->{ANA_ID} ... $analyst_record{$c->{ANA_ID} itself will return a ref.

Update
Sorry, I missed that you wanted to break up dates... for that add

$nr{Dates} = [split ',', $nr{Dates};
after the @nr{@keys} = @fields; line... that will put them in an array so $big_ash{13}{Dates}[0] will give you AM0
Also... using NR as a key won't work right if you have the same NR twice... which you seem to have, but is the exact same line...

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: Re: References for subroutines to Hashes in Hashes :)
by merlijn_u (Novice) on Aug 17, 2001 at 18:03 UTC
    damn thats great work of you...

    but no troubles with the -----

    I just putted it that way to give you a vision of the normalisation of my Query. I get my data from Oracle 8, so I just name th collumns....

    but my mayor question about the reference seems awsered so that should bring me further. Is my subroutine correct written, will be able to receive that reference and work with it?

    Much thanks
    Merlijn

      ahh yes, I'd missed the database calls...
      What are the fields your database returns? Your hash does look ok, but I would probably put the AM0 AM1 etc in an array, not a hash... something like...
      for $key (keys %$c) { next unless $key =~ /^[AP]M_\d+$/; push @{$a_record{$c->{ANA_ID}}{Dates}{$c->{ACTION_DATE}}}, $c->{$key} if $c->{$key}; }
      Not tested, but should work... that should go through the keys, matching any that start with AM or PM with an _ and the a number, and push them into an anonymous array if they have a value... this is assuming AM_0 AM_1 etc just hold a 1 or 0 for a yes or no... }

                      - Ant
                      - Some of my best work - Fish Dinner

        suaveant, I love ya :)

        your last awnser just helped me out, Thanks alot!!

        it's 18.24 PM here, so gonna quit, ah just such a relief.....

        have a nice weekend all, bye.

        Merlijn

        Good idea about that last array, but this are troubles for in an later stadia....

        I been coding just a bit and there still seems a trouble...

        lets say hashfilling is ok...

        while (my $c = $st->fetchrow_hashref) { my %analyst_record; $analyst_record{$c->{ANA_ID}}{e_mail}=$c->{EMAIL}; print STDERR "Hash: $analyst_record{$c->{ANA_ID}} \n"; Show_analyst_row($analyst_record{$c->{ANA_ID}}); ... ... ... sub Show_analyst_row { my(%analyst_record)=%{@_}; print STDERR "sub: $analyst_record{description} \n"; print "Valerie $analyst_record{description} \n"; print "</TR>\n"; }

        So I did remove the \ for the reference...

        But still he only prints "valerie" for every record in the query, Is it possible that there is still something wrong in the receiving of the reference cause when I just print and don't call the subfunction he does print the E-mail(uderstand what I mean?)

        here is the log of my errorlog(notice the prints I putted in the code for that)

        Hash: HASH(0x4039b1f0) sub: Hash: HASH(0x4039b1d8) sub: Hash: HASH(0x4037dfbc) sub: Hash: HASH(0x4039b3c4) sub: Hash: HASH(0x4039b3b8) sub: Hash: HASH(0x403aa028) sub: Hash: HASH(0x4039b214) sub:

        thanks for looking.
        Merlijn
        Good idea about that last array, but this are troubles for in a later stadia....

        I been coding just a bit and there still seems a trouble...

        lets say hashfilling is ok...

        while (my $c = $st->fetchrow_hashref) { my %analyst_record; $analyst_record{$c->{ANA_ID}}{e_mail}=$c->{EMAIL}; print STDERR "Hash: $analyst_record{$c->{ANA_ID}} \n"; Show_analyst_row($analyst_record{$c->{ANA_ID}}); ... ... ... sub Show_analyst_row { my(%analyst_record)=%{@_}; print STDERR "sub: $analyst_record{description} \n"; print "Valerie $analyst_record{description} \n"; print "</TR>\n"; }

        So I did remove the \ for the reference...

        But still he only prints "valerie" for every record in the query, Is it possible that there is still something wrong in the receiving of the reference cause when I just print and don't call the subfunction he does print the E-mail(uderstand what I mean?)

        here is the log of my errorlog(notice the prints I putted in the code for that)

        Hash: HASH(0x4039b1f0) sub: Hash: HASH(0x4039b1d8) sub: Hash: HASH(0x4037dfbc) sub: Hash: HASH(0x4039b3c4) sub: Hash: HASH(0x4039b3b8) sub: Hash: HASH(0x403aa028) sub: Hash: HASH(0x4039b214) sub:

        thanks for looking.
        Merlijn
        aye great tip Ant,

        I got for every key before I call the subroutine the correct value of the Hash. eg.

        1234 HASH(0x403aab28)
        but when I repeat this statement in the subroutine I receive nothing, completely nothing, how odd...

        sub Show_analyst_row { my(%analyst_record)=%{@_}; print STDERR (join "\n", %analyst_record); }

        above code generates nothing, I would think ther is something wrong in receiving the reference no?

        Any idea's?

        Thanks for looking
        Merlijn