in reply to How to remove the certain element of an object
Your OO design seems questionable - you seem to be using the Person class to represent individual people (good!) but also to represent collections of people/populations. Use two separate classes
package Nation { use Moose; has population => (is => 'ro', isa => 'ArrayRef[Person]'); ...; } package Person { use Moose; has name => (is => 'ro', isa => 'Str'); ...; }
I'm not 100% clear on exactly what you want to do. Do you want to remove people from the population when $person->need_to_go is true? If so, I'd probably do something like this:
use List::MoreUtils qw(part); # Split population into people to keep, and people to go... my ($keep, $go) = part { !!$_->need_to_go } @{ $people->population }; # Set the population to just the people we want to keep. @{ $people->population } = @$keep;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to remove the certain element of an object
by vagabonding electron (Curate) on Feb 04, 2013 at 10:09 UTC | |
Thank you very much tobyink ! I will work further with this. The background of this - I am trying to translate some Ruby scripts from this book into Perl Exploring Everyday Things ... . There is a free sampler on O'Reilly exactly with the part I am trying to translate - the direct link to the sampler seems not to working but here is the main page of the book http://shop.oreilly.com/product/0636920022626.do . It deals with the Monte Carlo simulation to model the restroom usage in an office. You observe the queues which are formed in the restroom dependent on the number of the people in the group and the number of facilities in the restroom etc.Well it is probably bold of me to try to do so since I do not speak Ruby at all and I am still a beginner in Perl. Otherwise I thought it is dumb just to install Ruby and copy the scripts mechanically. BTW the class Person there in the book represents the group and the individuals - I found this strange at first but I thought if adults do so I could try it too :-) Thank you for your proposal to use the separate classes! In fact I have a working attempt that seems to be similar to the original but not in OO-way. I am trying to do first steps in the OO-programming and I thought this exercise could be educational. BTW here is my "working attempt" (tried to wrap it in the spoiler to save place but failed).
| [reply] [d/l] |
by tobyink (Canon) on Feb 04, 2013 at 15:06 UTC | |
I'm no Ruby expert, but I think you're misinterpreting the example in the book. @@population is a "class attribute", not an object attribute. In other words, it's a property of the "Person" class; not a property of each Person. Using Moose you could model it along these lines:
Though there are many reasons to avoid class attributes. (Basically think of all the good reasons to avoid global variables, and then s/global variables/class attributes/gi.)
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
| [reply] [d/l] [select] |
by vagabonding electron (Curate) on Feb 04, 2013 at 16:38 UTC | |
| [reply] |
Re^2: How to remove the certain element of an object # Seems to work now!
by vagabonding electron (Curate) on Feb 04, 2013 at 15:50 UTC | |
Dear tobyink thanks to your and Athanasius great help I was able to go further and make a script that seems to bring a reasonable output (if I do not miss something). Here is the script, thank you very much again and it would be great if you could comment this one too :-) : Thanks again! VE
| [reply] [d/l] |