size of an array via a ref to it

Discussion in 'Perl Misc' started by dn.perl@gmail.com, Feb 23, 2012.

  1. Guest

    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
    , Feb 23, 2012
    #1
    1. Advertising

  2. On 23 Feb., 07:42, "" <> wrote:
    > 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
    Markus Hutmacher, Feb 23, 2012
    #2
    1. Advertising

  3. wrote:
    >
    > 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
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Feb 23, 2012
    #3
  4. Dr.Ruud Guest

    On 2012-02-23 14:23, John W. Krahn wrote:

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


    I tend to write that as:

    s/\s+\z//, s/\A\s+// for @arr2;

    but I don't think it matters much.

    --
    Ruud
    Dr.Ruud, Feb 23, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    337
  2. Replies:
    22
    Views:
    754
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    346
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    143
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    613
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page