D
dn.perl
The first patch of code works, but the second one (below the === line
below) does not.
The line marked 'this line should work, too' was temporarily bypassed
by hard-coding a value.
I have an array, and the intention is to remove the leading/trailing
whitespace from each element of the array.
So, [" one ", " Peter "] -- should become -- ["one",
"Peter"] .
I tried reading perlreftut but did not find the answer.
#!/usr/local/bin/perl
use strict ;
use warnings ;
my @arr2 = (" one ", " two ") ;
trim_array(@arr2) ;
sub trim_array
{
my @arr2 = @_ ;
my $size01 = $#arr2 + 1;
print "size-of-array is $size01\n" ;
for(my $ii = 0; $ii < $size01; $ii++) {
$arr2[$ii] =~ s/\s*(.*)\s*/$1/ ;
print "element is $arr2[$ii] \n" ;
}
}
===============
= = = = = = =
#!/usr/local/bin/perl
use strict ;
use warnings ;
my @arr2 = (" one ", " two ") ;
trim_array(\@arr2) ; ## passing the reference to the array this time
sub trim_array
{
my $ra = @_ ;
## my $size01 = $#{$ra} + 1; ## this line should work, too
my $size01 = 2 ;
print "size-of-array is $size01\n" ;
for(my $ii = 0; $ii < $size01; $ii++) {
${$ra}[$ii] =~ s/\s*(.*)\s*/$1/ ;
print "element is ${$ra}[$ii] \n" ;
}
}
Error: Can't use string ("1") as an ARRAY ref while "strict refs" in
use
below) does not.
The line marked 'this line should work, too' was temporarily bypassed
by hard-coding a value.
I have an array, and the intention is to remove the leading/trailing
whitespace from each element of the array.
So, [" one ", " Peter "] -- should become -- ["one",
"Peter"] .
I tried reading perlreftut but did not find the answer.
#!/usr/local/bin/perl
use strict ;
use warnings ;
my @arr2 = (" one ", " two ") ;
trim_array(@arr2) ;
sub trim_array
{
my @arr2 = @_ ;
my $size01 = $#arr2 + 1;
print "size-of-array is $size01\n" ;
for(my $ii = 0; $ii < $size01; $ii++) {
$arr2[$ii] =~ s/\s*(.*)\s*/$1/ ;
print "element is $arr2[$ii] \n" ;
}
}
===============
= = = = = = =
#!/usr/local/bin/perl
use strict ;
use warnings ;
my @arr2 = (" one ", " two ") ;
trim_array(\@arr2) ; ## passing the reference to the array this time
sub trim_array
{
my $ra = @_ ;
## my $size01 = $#{$ra} + 1; ## this line should work, too
my $size01 = 2 ;
print "size-of-array is $size01\n" ;
for(my $ii = 0; $ii < $size01; $ii++) {
${$ra}[$ii] =~ s/\s*(.*)\s*/$1/ ;
print "element is ${$ra}[$ii] \n" ;
}
}
Error: Can't use string ("1") as an ARRAY ref while "strict refs" in
use