iterating through perl object fields

Discussion in 'Perl Misc' started by Mafj, Nov 13, 2008.

  1. Mafj

    Mafj Guest

    Hi,

    I am a complete beginner I tried to find some information on the
    topic, however, I have only found how to deal with arrays and
    associative arrays. All documentation seems to be helpless with an
    object/structure.

    I have a fixed structure like this (comes from external file):

    $self->{data} = {
    'ver' => 2,
    'list' => {
    'name1' => 'item1',
    'different_name' => 'another_item',
    'whatever' => 'special_item',
    # and 1000 more
    }
    };
    I need a content of $self->{data}->{list}. I do knot know field names
    nor keys.
    There is probably some simple loop, foreach or while,e.g.
    ( ($c,$v) = each %self->{data}->{list} ).
    But I cannot figure out the details that would finally let it work.
    Help...
     
    Mafj, Nov 13, 2008
    #1
    1. Advertising

  2. Mafj <> writes:

    > I have a fixed structure like this (comes from external file):
    >
    > $self->{data} = {
    > 'ver' => 2,
    > 'list' => {
    > 'name1' => 'item1',
    > 'different_name' => 'another_item',
    > 'whatever' => 'special_item',
    > # and 1000 more
    > }
    > };
    > I need a content of $self->{data}->{list}. I do knot know field names
    > nor keys.
    > There is probably some simple loop, foreach or while,e.g.
    > ( ($c,$v) = each %self->{data}->{list} ).


    You're pretty close. To begin with, since $self->{data}->{list} is a
    reference to a hash, you need to dereference it with %$ in order to work
    with the hash it refers to.

    And second, you can get a list of hash keys with the keys (perldoc -f
    keys) function:

    my @listKeys = keys(%$self->{data}->{list});

    sherm--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Nov 13, 2008
    #2
    1. Advertising

  3. Sherm Pendley wrote:
    > Mafj <> writes:
    >
    >> I have a fixed structure like this (comes from external file):
    >>
    >> $self->{data} = {
    >> 'ver' => 2,
    >> 'list' => {
    >> 'name1' => 'item1',
    >> 'different_name' => 'another_item',
    >> 'whatever' => 'special_item',
    >> # and 1000 more
    >> }
    >> };
    >> I need a content of $self->{data}->{list}. I do knot know field names
    >> nor keys.
    >> There is probably some simple loop, foreach or while,e.g.
    >> ( ($c,$v) = each %self->{data}->{list} ).

    >
    > You're pretty close. To begin with, since $self->{data}->{list} is a
    > reference to a hash, you need to dereference it with %$ in order to work
    > with the hash it refers to.
    >
    > And second, you can get a list of hash keys with the keys (perldoc -f
    > keys) function:
    >
    > my @listKeys = keys(%$self->{data}->{list});


    ITYM: my @listKeys = keys %{$self->{data}->{list}};

    $ perl -le'
    my $self = { data => { list => { "a" .. "f" } } };
    print for keys %$self->{data}->{list};
    '
    Type of arg 1 to keys must be hash (not hash element) at -e line 3, near
    "};"
    Execution of -e aborted due to compilation errors.




    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Nov 14, 2008
    #3
  4. "John W. Krahn" <> writes:

    > Sherm Pendley wrote:
    >> Mafj <> writes:
    >>
    >>> I have a fixed structure like this (comes from external file):
    >>>
    >>> $self->{data} = {
    >>> 'ver' => 2,
    >>> 'list' => {
    >>> 'name1' => 'item1',
    >>> 'different_name' => 'another_item',
    >>> 'whatever' => 'special_item',
    >>> # and 1000 more
    >>> }
    >>> };
    >>> I need a content of $self->{data}->{list}. I do knot know field names
    >>> nor keys.
    >>> There is probably some simple loop, foreach or while,e.g.
    >>> ( ($c,$v) = each %self->{data}->{list} ).

    >>
    >> You're pretty close. To begin with, since $self->{data}->{list} is a
    >> reference to a hash, you need to dereference it with %$ in order to work
    >> with the hash it refers to.
    >>
    >> And second, you can get a list of hash keys with the keys (perldoc -f
    >> keys) function:
    >>
    >> my @listKeys = keys(%$self->{data}->{list});

    >
    > ITYM: my @listKeys = keys %{$self->{data}->{list}};


    Yep, you're right. That's what I get for banging out code in a post
    without testing it. :-(

    sherm--

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Nov 14, 2008
    #4
  5. Mafj

    Mafj Guest

    Thanks guys the following finally work:

    eval `cat ./datafile.pl`;
    foreach $x( keys %{$self->{data}->{list} } )
    {
    print "$x $self->{data}->{list}->{$x}\n";
    }
    Once again thanks for dropping me line.

    Regards,
    Maciej
     
    Mafj, Nov 14, 2008
    #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. Replies:
    4
    Views:
    463
    Steven Bethard
    Feb 17, 2005
  2. carl
    Replies:
    5
    Views:
    2,381
    James Kanze
    Nov 25, 2009
  3. Lise
    Replies:
    0
    Views:
    113
  4. Paxton
    Replies:
    4
    Views:
    180
    Paxton
    Nov 22, 2004
  5. Peter Bailey
    Replies:
    6
    Views:
    130
    Peter Bailey
    Feb 16, 2007
Loading...

Share This Page