Datastructure

Discussion in 'Perl Misc' started by Bioperler, Nov 15, 2007.

  1. Bioperler

    Bioperler Guest

    HI!

    Actually I'm about to code a tool, that's able to track users of a
    website (using cookies). However, I've got a problem with the sorting of
    the data. I need a data structure like this:

    %hash(array(array)) "hash of an array of an array"

    So for testing purposes I generated a small dataset, which looks like this:


    %HoAoA = (
    hash1 => [
    ( [ "first", "second" ],
    [ "third" , "fourth" ],
    [ "fifth" , "sixth" ]
    )
    ],
    hash2 => [
    ( [ "9th", "10th" ],
    [ "11th" , "12th" ]
    )
    ]

    );

    So while it was no problem to acces all data fields using nested loops,
    I was not able to append data to these arrays listed above. Array's push
    doesn't seem to work for this.

    Example of what I want to do:
    push($HoAoA{hash1}[2], "seventh");


    Any suggestions?
     
    Bioperler, Nov 15, 2007
    #1
    1. Advertising

  2. Bioperler <> writes:

    > %HoAoA = (
    > hash1 => [
    > ( [ "first", "second" ],
    > [ "third" , "fourth" ],
    > [ "fifth" , "sixth" ]
    > )
    > ],


    You don't need the inner ()'s

    > hash2 => [
    > ( [ "9th", "10th" ],
    > [ "11th" , "12th" ]
    > )
    > ]
    >
    > );
    >


    > Example of what I want to do:
    > push($HoAoA{hash1}[2], "seventh");


    The first argument to push should be an actual array and not just an
    reference. So you have to dereference it:

    push @{ $HoAoA{hash1}[2] }, "seventh";

    //Makholm
     
    Peter Makholm, Nov 15, 2007
    #2
    1. Advertising

  3. Bioperler wrote:
    >
    > Actually I'm about to code a tool, that's able to track users of a
    > website (using cookies). However, I've got a problem with the sorting of
    > the data. I need a data structure like this:
    >
    > %hash(array(array)) "hash of an array of an array"
    >
    > So for testing purposes I generated a small dataset, which looks like this:
    >
    > %HoAoA = (
    > hash1 => [
    > ( [ "first", "second" ],
    > [ "third" , "fourth" ],
    > [ "fifth" , "sixth" ]
    > )
    > ],
    > hash2 => [
    > ( [ "9th", "10th" ],
    > [ "11th" , "12th" ]
    > )
    > ]
    >
    > );
    >
    > So while it was no problem to acces all data fields using nested loops,
    > I was not able to append data to these arrays listed above. Array's push
    > doesn't seem to work for this.
    >
    > Example of what I want to do:
    > push($HoAoA{hash1}[2], "seventh");
    >
    > Any suggestions?


    You have to dereference the array (push only modifies arrays):

    push @{ $HoAoA{ hash1 }[ 2 ] }, 'seventh';



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 15, 2007
    #3
  4. Bioperler

    Bioperler Guest

    Peter Makholm wrote:
    > Bioperler <> writes:
    >
    >> %HoAoA = (
    >> hash1 => [
    >> ( [ "first", "second" ],
    >> [ "third" , "fourth" ],
    >> [ "fifth" , "sixth" ]
    >> )
    >> ],

    >
    > You don't need the inner ()'s

    Yes, you're right. But actually the structure is a bit more complex - I
    just shorted it a little bit to ease it.

    >
    >> hash2 => [
    >> ( [ "9th", "10th" ],
    >> [ "11th" , "12th" ]
    >> )
    >> ]
    >>
    >> );
    >>

    >
    >> Example of what I want to do:
    >> push($HoAoA{hash1}[2], "seventh");

    >
    > The first argument to push should be an actual array and not just an
    > reference. So you have to dereference it:
    >
    > push @{ $HoAoA{hash1}[2] }, "seventh";

    Thanks! This works. Seems as I have to read a little bit about that :)
     
    Bioperler, Nov 15, 2007
    #4
  5. Bioperler <> wrote:
    > Peter Makholm wrote:



    >> push @{ $HoAoA{hash1}[2] }, "seventh";

    > Thanks! This works. Seems as I have to read a little bit about that :)



    You can do that with:

    perldoc perlreftut


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Nov 16, 2007
    #5
    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. Anony!

    Set DataStructure

    Anony!, Aug 12, 2004, in forum: Java
    Replies:
    2
    Views:
    409
    Anony!
    Aug 13, 2004
  2. Sharp

    Index-based datastructure

    Sharp, Mar 14, 2005, in forum: Java
    Replies:
    1
    Views:
    342
    Chris Uppal
    Mar 14, 2005
  3. Replies:
    3
    Views:
    395
    shakah
    Jun 23, 2005
  4. Santosh

    Datastructure design

    Santosh, Nov 19, 2003, in forum: C Programming
    Replies:
    6
    Views:
    389
    pandy
    Nov 20, 2003
  5. Prateek Basu

    Datastructure and Algorithms

    Prateek Basu, Jan 23, 2004, in forum: C Programming
    Replies:
    4
    Views:
    2,218
    Prateek Basu
    Jan 24, 2004
Loading...

Share This Page