programmer.perl has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I'm having trouble with a deleting the element of an array, here I'm attaching the output (error in the last line, if you see there are two commas), it seems that element is not being deleted, instead it's undefined (func. undef). How to delete the element (from memory)? I tried to look into perldoc but I couldn't find another functions except 'delete' and 'undef'. Here in perldoc about 'delete' : WARNING: Calling delete on array values is deprecated and likely to be removed in a future version of Perl.
Output of program:
Here are the statistics for the Sino. Address: kk ID: kk <...continues...> Course(s): Chem, Bio, Maths, C++, Java After dropping: Chem, Bio, Maths, , Java
Code of program and module:
package Student3; sub new { my $class = shift; my $ref = {}; # Anonymous hash bless($ref, $class); return $ref; } sub set_student { my $self = shift; <...continues...> print "Enter the student's course(s) delimiting them with a comma (,) "; chomp(my $cour = <STDIN>); $cour =~ s/\s+//g; my @cour = split(",", $cour); chomp($self->{"Course(s)"}->{"cour"}=[@cour]); <...continues here...> } <...continues...> sub drop_courses { my $self=shift; my $del_cour = shift; # $student1->drop_courses(["C++"]); my $num = @{$del_cour}; if ($num == 1) { foreach $k (@{$self->{'Course(s)'}->{'cour'}}) { if ($k eq $del_cour->[0]) { $k=""; } } } print "After dropping: ", join (", ", @{$self->{'Course(s)'}->{'cour'}}), "\n"; 1;
The code of a program
#!/usr/bin/perl -w use Student3; use warnings; use strict; my $student1 = Student3->new; $student1->set_student; $student1->show_student; $student1->add_courses(["C++", "Java"]); $student1->show_student; $student1->drop_courses(["C++"]);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: deleting from the memory
by davido (Cardinal) on Jul 23, 2012 at 23:33 UTC | |
Re: deleting from the memory
by roboticus (Chancellor) on Jul 23, 2012 at 23:48 UTC | |
Re: deleting from the memory
by kcott (Archbishop) on Jul 24, 2012 at 00:42 UTC | |
by programmer.perl (Beadle) on Jul 24, 2012 at 01:14 UTC |
Back to
Seekers of Perl Wisdom