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:
Thank you, Ken!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"; }
|
---|