Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Looping through Class::Accessor accessors

Reply
Thread Tools

Looping through Class::Accessor accessors

 
 
Derek Basch
Guest
Posts: n/a
 
      06-20-2006
Hi Everyone,

I began using Class:BI and love it. However, there are situations
where I want to loop through the Class::Accessor accessors for an
object and also get the name of the accessor as I loop through.

For instance here is some psuedo-perl that I wrote to visualize what I
need:

# returns an array of objects
my @gpsaccounts = Database::Members::Gpsaccounts->search($user_id);

# loop through the array of objects
foreach my @row_class (@gpsaccounts) {
# loop through the objects accessors
foreach $class_accessor (@row_class) {
# print the accessors name and value
print "$class_accessor_name: $class_accessor_value\n"
}
}

Yes, I know that the above is syntactically unpossible but I don't know
how else to express it.

Here's how you would normally do it:

my @gpsaccounts =
Database::Members::Gpsaccounts->search_gpsaccounts($user_id);

foreach(@gpsaccounts) {
print $_->fullname . "\n";
print $_->user . "\n";
print $_->email . "\n";
print "------------------------" . "\n";
}

Any ideas? I am a newer Perl programmer so any help is appreciated.

Thanks,
Derek Basch

 
Reply With Quote
 
 
 
 
Derek Basch
Guest
Posts: n/a
 
      06-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Derek Basch wrote:
> > Hi Everyone,
> >
> > I began using Class:BI and love it. However, there are situations
> > where I want to loop through the Class::Accessor accessors for an
> > object and also get the name of the accessor as I loop through.


Thanks for all the help Steven.

However, the solution suddenly occured to me last night. I was
declaring my accessors with the 'columns' function of the Class:BI
module. Therefore, there must be a hash or something somewhere to loop
over. I found that Class:BI objects have various column retrieval
functions. I used the 'all_columns' function that returns the column
names. Works great .


sub test {
my @gpsaccounts = Members::Gpsaccounts->search($user_id);
foreach my $row (@gpsaccounts) {
foreach my $column ($row->all_columns()) {
print $column . "\n";
}
}

Derek Basch

 
Reply With Quote
 
 
 
 
Derek Basch
Guest
Posts: n/a
 
      06-21-2006
> However, the solution suddenly occured to me last night. I was
> declaring my accessors with the 'columns' function of the Class:BI
> module. Therefore, there must be a hash or something somewhere to loop
> over. I found that Class:BI objects have various column retrieval
> functions. I used the 'all_columns' function that returns the column
> names. Works great .
>
>
> sub test {
> my @gpsaccounts = Members::Gpsaccounts->search($user_id);
> foreach my $row (@gpsaccounts) {
> foreach my $column ($row->all_columns()) {
> print $column . "\n";
> }
> }
>
> Derek Basch



Here's an improved version:

sub test {
my @gpsaccounts = Members::Gpsaccounts->search($user_id);
foreach my $row (@gpsaccounts) {
foreach my $column ($row->columns()) {
print $row->$column . "\n";
}
}

 
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
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
question about accessors and constants and variable declaration locations Flip Java 2 05-25-2004 02:28 PM
Do accessors lower performance? Zhao Java 3 10-11-2003 07:53 AM
Accessors by value or reference? Jarma C++ 0 08-29-2003 08:26 PM



Advertisments