debugging and @array

ignoRamous
ignoRamous used Ask the Experts™
on
My array @num is printing something else and I'm not sure why it's doing that?
For example I input: 1 abc 1.2 -1 .5 1.3a 1.a1.
Whatever I input always has the same thing as the $num. But when I split it and assign each individual number or string, when I print the @num I get an output of 1, which I know it is wrong.  
I also need some help on how to delete an element in the array?
Isn't UNLESS is exactly the same as the ELSE of the IF statement?
Any suggestion?

print "Please enter a number series of number\n\t(separated space):";
$num=<STDIN>;
#$num=chomp($num);
@num = split(/\s/, chomp($num));

print "$num\n";
print "@num \n";
#printing both scalar and array variable to make sure they are the same

foreach $val (@num){
            #checking for the invalid number
     unless ($val =~ /^-?\d*(\d\.|\.\d)\d*$/)
          {
                  shift;
        #if the $val is invalid then I want to delete that number or element in the @num array.
        #Does SHIFT func delete a particular element in the array?    
     }
}
print "The VALID# are:\t@num\n";
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I already solve some of my problems, the only help or suggestion that I need is how to delete an elemet in the @array?

Author

Commented:
Can anybody help me debug this code?
I'm calculating the right way but I'm getting the wrong answer.

@n=(5,15,5,6);
$x=first(@n);
print "$x";
#when I print the value of $x, it's giving me the wrong answer

sub example
{
     $sum=0;
     for($i=0;$i<@_;$i++)
          {
          $sum+=@_[i];
          }
     return $sum;
# $sum should contain the value of 31
}
@n=(5,15,5,6);
$x=$n[0];
print "$x";

print "\n",  example();
print "\n",  example2();
print "\n",  example3();

sub example
{
    $sum=0;
    for($i = 0; $i <= $#n; $i++)
         {
         $sum += $n[$i];
         }
    return $sum;
}

sub example2
{
    foreach (@n)
         {
         $sum2 += $_;
         }
    return $sum2;
}

sub example3
{
    $sum3 += $_ for @n;
    return $sum3;
}
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Thankx PC USER321 but I already fix my code and the onLy help I need is how to delete an elemet in the @array?
@n=(5,15,5,6, 15, 15, 8, 15);
for($i = $#n; $i >= 0; $i--)
{
    splice(@n, $i, 1) if $n[$i] == 15;
}

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept a comment by PC_User321

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

inq123
EE Cleanup Volunteer
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
@n = grep $_!=15,@n

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial