Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Nested Data Structures, OOP

by programmer.perl (Beadle)
on Jul 24, 2012 at 14:30 UTC ( #983429=note: print w/replies, xml ) Need Help??


in reply to Re^3: Nested Data Structures, OOP
in thread Nested Data Structures, OOP

Hi Ken,

The grep function helped me to delete the elements of an array. In my book (Perl by Example 4th edit.) there is no format of grep function, which you show me:

@x = grep { $_ ne q{b} } @x;

In the book, there is only one format: grep(EXPR,LIST). I used your format and it is working :-). Both calls: with single or multiple arguments are working:

$student1->drop_courses(["C++"]); $student1->drop_courses(["C++", "Bio"]);

Here is the final script of a sub:

sub drop_courses { # drop_courses(["",]); my $self=shift; my $del_cour = shift; # $del_cour = ["Java"]; my $num = @{$del_cour}; if ($num == 1) { @{$self->{'Course(s)'}->{'cour'}} = grep { $_ ne $del_cour->[0] } @{$self->{'Course(s)'}->{'cour'}}; print "After dropping: ", join (", ", @{$self->{'Course(s)'}->{'cour'}}), "\n"; } elsif ($num > 1) { for ($i=0; $i<$num; $i++) { @{$self->{'Course(s)'}->{'cour'}} = grep { $_ ne $del_cour->[$i] } @{$self->{'Course(s)'}->{'cour'}}; } } print "After dropping: ", join (", ", @{$self->{'Course(s)'}->{'cour'}}), "\n"; }
Thank you, Ken!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983429]
help
Chatterbox?
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.
[TStanley]: I am heading to Arlington, Virginia next week, then I get to spend an entire week at home. Looking forward to that as I will have spent 7 weeks on the road by the end of next week
[talexb]: That's a long road trip. I haven't travelled for business in years.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-06-23 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (548 votes). Check out past polls.