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

Re: [PBML] Can't modify private array in concatenation (.) or string

Expand Messages
  • 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 1 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.