Loading ...
Sorry, an error occurred while loading the content.

Can't modify private array in concatenation (.) or string

Expand Messages
  • drberg1000
    I ve boiled a problem in a script I m writing down to the following code: #!/usr/bin/perl my @array = { string a - , string b - }; @array .= my_sub(); print
    Message 1 of 4 , Apr 12, 2011
    • 0 Attachment
      I've boiled a problem in a script I'm writing down to the following
      code:

      #!/usr/bin/perl
      my @array = {"string a - ", "string b - "};
      @array .= my_sub();
      print $array[0];
      print $array[1];

      sub my_sub{
      return "string 1\n", "string2 \n";
      }

      that when run gives:
      Can't modify private array in concatenation (.) or string at ./test.pl
      line 3, near ");"
      Execution of ./test.pl aborted due to compilation errors.

      What I want for a result is:
      string a - string 1
      string b - string 2

      I know I could rewrite
      @array .= my_sub();
      as:
      @temp .= my_sub();
      $array[0] .= $temp[0];
      $array[1] .= $temp[1];

      but in the full script the line gets inserted into a switch something
      like:
      case /Array 1/ { $array1 .= my_sub(@columns); }
      case /Array 2/ { $array2 .= my_sub(@columns); }
      case /Array 3/ { $array3 .= my_sub(@columns); }
      #7 more cases and an else.
      so it'd be really nice to keep it as a short one liner. Seems like
      there should be an elegant way to do this.

      Any advice?

      --Dave



      [Non-text portions of this message have been removed]
    • Tim Lewis
      Someone else might have a better answer, but this works: use strict; use warnings; my @array = ( string a - , string b - ); my $arraytotal = @array; my
      Message 2 of 4 , Apr 13, 2011
      • 0 Attachment
        Someone else might have a better answer, but this works:


        use strict;
        use warnings;
        my @array = ("string a - ", "string b - ");

        my $arraytotal = @array;
        my @my_sub_data = &my_sub;

        for (my $count=0;$count<$arraytotal;$count++) {
        $array[$count] .= $my_sub_data[$count];
        }
        #@array .= my_sub();
        print $array[0];
        print $array[1];

        sub my_sub{
        return "string 1\n", "string2 \n";
        }

        ---- drberg1000 <drberg1000@...> wrote:
        > I've boiled a problem in a script I'm writing down to the following
        > code:
        >
        > #!/usr/bin/perl
        > my @array = {"string a - ", "string b - "};
        > @array .= my_sub();
        > print $array[0];
        > print $array[1];
        >
        > sub my_sub{
        > return "string 1\n", "string2 \n";
        > }
        >
        > that when run gives:
        > Can't modify private array in concatenation (.) or string at ./test.pl
        > line 3, near ");"
        > Execution of ./test.pl aborted due to compilation errors.
        >
        > What I want for a result is:
        > string a - string 1
        > string b - string 2
        >
        > I know I could rewrite
        > @array .= my_sub();
        > as:
        > @temp .= my_sub();
        > $array[0] .= $temp[0];
        > $array[1] .= $temp[1];
        >
        > but in the full script the line gets inserted into a switch something
        > like:
        > case /Array 1/ { $array1 .= my_sub(@columns); }
        > case /Array 2/ { $array2 .= my_sub(@columns); }
        > case /Array 3/ { $array3 .= my_sub(@columns); }
        > #7 more cases and an else.
        > so it'd be really nice to keep it as a short one liner. Seems like
        > there should be an elegant way to do this.
        >
        > Any advice?
        >
        > --Dave
        >
        >
        >
        > [Non-text portions of this message have been removed]
        >
      • Brian F. Yulga
        Hm, I may not understand the problem correctly. I m not sure if @array ever has more than two items, or if &my_sub needs to take this into account (string 3,
        Message 3 of 4 , Apr 13, 2011
        • 0 Attachment
          Hm, I may not understand the problem correctly. I'm not sure if @array
          ever has more than two items, or if &my_sub needs to take this into
          account (string 3, string 4, etc.?). Also, why use ".=" for the sub
          call? (again, maybe I'm missing the point.) Unless you specifically
          need to pass the array by reference (if array is large), couldn't you do
          this?......

          #!perl -wl
          use strict;

          sub my_sub {
          return ( shift . q(string 1), shift . q(string 2) );
          }

          my @array = ( q(string a - ), q(string b - ) );
          @array = my_sub @array;

          print foreach @array;

          #### or, if you don't want to chop off @array for having more than 2 items:

          sub my_sub {
          my @subarray = @_;
          @subarray[0, 1] = ( $subarray[0] . q(string 1), $subarray[1] .
          q(string 2) );
          return @subarray;
          }

          my @array = ( q(string a - ), q(string b - ), q(string c - ) );
          @array = my_sub @array;

          print foreach @array;


          Tim Lewis wrote:
          >
          > Someone else might have a better answer, but this works:
          >
          > use strict;
          > use warnings;
          > my @array = ("string a - ", "string b - ");
          >
          > my $arraytotal = @array;
          > my @my_sub_data = &my_sub;
          >
          > for (my $count=0;$count<$arraytotal;$count++) {
          > $array[$count] .= $my_sub_data[$count];
          > }
          > #@array .= my_sub();
          > print $array[0];
          > print $array[1];
          >
          > sub my_sub{
          > return "string 1\n", "string2 \n";
          > }
          >
          > ---- drberg1000 <drberg1000@... <mailto:drberg1000%40gmail.com>>
          > wrote:
          > > I've boiled a problem in a script I'm writing down to the following
          > > code:
          > >
          > > #!/usr/bin/perl
          > > my @array = {"string a - ", "string b - "};
          > > @array .= my_sub();
          > > print $array[0];
          > > print $array[1];
          > >
          > > sub my_sub{
          > > return "string 1\n", "string2 \n";
          > > }
          > >
          > > that when run gives:
          > > Can't modify private array in concatenation (.) or string at ./test.pl
          > > line 3, near ");"
          > > Execution of ./test.pl aborted due to compilation errors.
          > >
          > > What I want for a result is:
          > > string a - string 1
          > > string b - string 2
          > >
          > > I know I could rewrite
          > > @array .= my_sub();
          > > as:
          > > @temp .= my_sub();
          > > $array[0] .= $temp[0];
          > > $array[1] .= $temp[1];
          > >
          > > but in the full script the line gets inserted into a switch something
          > > like:
          > > case /Array 1/ { $array1 .= my_sub(@columns); }
          > > case /Array 2/ { $array2 .= my_sub(@columns); }
          > > case /Array 3/ { $array3 .= my_sub(@columns); }
          > > #7 more cases and an else.
          > > so it'd be really nice to keep it as a short one liner. Seems like
          > > there should be an elegant way to do this.
          > >
          > > Any advice?
          > >
          > > --Dave
          > >
          > >
          > >
          > > [Non-text portions of this message have been removed]
          > >
          >
          > .
          >
          >
        • Charles K. Clarkson
          ... You are using a hash operator {} in an array assignment. You will end up with an array of hashes, not an array. Always, always, always test your
          Message 4 of 4 , Apr 14, 2011
          • 0 Attachment
            On 4/12/2011 3:21 PM, drberg1000 wrote:

            > #!/usr/bin/perl
            > my @array = {"string a - ", "string b - "};

            You are using a hash operator {} in an array assignment. You will end
            up with an array of hashes, not an array.

            Always, always, always test your assumptions:

            #!/usr/bin/perl

            use strict;
            use warnings;

            use Data::Dumper 'Dumper';

            my @array = { "string a - ", "string b - " };

            print Dumper \@array;

            __END__

            Using an array operator () the result is different:

            #!/usr/bin/perl

            use strict;
            use warnings;

            use Data::Dumper 'Dumper';

            my @array = ( 'string a - ', 'string b - ' );

            print Dumper \@array;

            __END__

            > @array .= my_sub();
            >
            > sub my_sub{
            > return "string 1\n", "string2 \n";
            > }
            >
            > [...]
            >
            > What I want for a result is:
            > string a - string 1
            > string b - string 2

            The output of my_sub() (You didn't really name it that, did you?) is
            not affected by the values in @array. What makes you think they would
            be? my_sub() will always return the exact same thing. Test it.

            You might try reading up on perl subroutines:

            http://www.google.com/search?q=perl+sub+pass+array



            HTH,

            Charles Clarkson
            --
            Mobile Home Investor
            Free Market Advocate
            Programmer

            I'm not really a smart person. I just play one on the Internet.

            Stephenville, TX
            http://twitter.com/CharlesClarkson
            +1 (254) 968-8328
          Your message has been successfully submitted and would be delivered to recipients shortly.