Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > iterating through perl object fields

Reply
Thread Tools

iterating through perl object fields

 
 
Mafj
Guest
Posts: n/a
 
      11-13-2008
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...




 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      11-13-2008
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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-14-2008
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
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      11-14-2008
"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
 
Reply With Quote
 
Mafj
Guest
Posts: n/a
 
      11-14-2008
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

 
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
Iterating a std::vector vs iterating a std::map? carl C++ 5 11-25-2009 09:55 AM
Iterating through 2 fields for one hash, or array. Peter Bailey Ruby 6 02-16-2007 02:48 PM
newbie question - iterating through dictionary object mirandacascade@yahoo.com Python 4 02-17-2005 04:54 PM
Iterating through a collection of Request.Form fields Paxton ASP General 4 11-22-2004 10:20 PM
FileSystem Object - Iterating through a directory and building the path Lise ASP General 0 11-17-2003 09:07 PM



Advertisments