size of an array via a ref to it

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
 
M

Markus Hutmacher

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

the parameter for "trim_array" in the second case is a variable
instead of an array.
You should use
my $ra = shift ;

Markus
 
J

John W. Krahn

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" ;
}
}

That is usually written as:

my @arr2 = ( " one ", " two " );

s/^\s+//, s/\s+$// for @arr2;



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top