2

Minimal code based on this answer and its join statement

my @x = qw/10 20 30 40/;
my @y = qw/60 70 8 90 10/;
my @input_list = (@x, @y);

print "Before join @input_list \n";

print join ",", @$_ for @input_list ;

print "After join @input_list \n";

which gives

Before join 20 40 60 80 120 140 16 180 20 
After join 20 40 60 80 120 140 16 180 20 

but in use strict;

Can't use string ("10") as an ARRAY ref while "strict refs" in use at test4.pl line 10.

join joins separate strings of array in manual. Here the code tries to join comma apparently with each hash (@$_) of the array item. However, this seems to be happening.

Why is this error here in the minimum code?

4
  • 1
    Your entire statement @$_ for @input_list ; is both incorrect and unnecessary. join accepts a delimiter and a list, just pass @input_list directly to join. Commented May 14, 2015 at 14:17
  • @HunterMcMillen @masi in your previous question @intercept_list was an reference to an array of anonymous arrays (built from @input_list) and you were using join on the de-referenced elements - i.e @$_. Commented May 14, 2015 at 15:26
  • 1
    In general, rather than stringing together a series of very long questions you should focus your posts in a way that helps you understand the problem - and then focus on understanding the solution. This question is better than the one you refer to (which won't get a lot of up votes in its current form - I hope you can improve it). I speak from my own experience of withdrawn and down voted questions here, but I do think keeping questions short and focused helps improve the quality of SO resources: both questions and answers. Commented May 14, 2015 at 15:28
  • I am just as guilty for following up with too numerous comments in the other thread. I am going to remove them and the ones I have made here after 24 hours. mea culpa Commented May 14, 2015 at 15:34

2 Answers 2

7

OK, what you're doing here:

print join ",", @$_ for @input_list ;

Isn't working, because it's:

  • iterating @input_list extracting each element into $_.
  • Dereferencing $_ pretending it's an array @$_.

This is basically the same as trying to:

print join ( ",", @{"10"} );

Which makes no sense, and so doesn't work.

my $string = join ( ",", @input_list );
print $string; 

Will do the trick.

The thing that you're missing here I think, is this:

use Data::Dumper; 
my @x = qw/10 20 30 40/;
my @y = qw/60 70 8 90 10/;
my @input_list = (@x, @y);
print Dumper \@input_list;

Isn't generating a multi-dimensional list. It's a single dimensional one.

$VAR1 = [
          '10',
          '20',
          '30',
          '40',
          '60',
          '70',
          '8',
          '90',
          '10'
        ];

I suspect what you may want is:

my @x = qw/10 20 30 40/;
my @y = qw/60 70 8 90 10/;
my @input_list = (\@x, \@y);

Or perhaps:

my $x_ref = [ qw/10 20 30 40/ ];
my $y_ref = [ qw/60 70 8 90 10/ ];
my @input_list = ($x_ref, $y_ref );

Which makes @input_list:

$VAR1 = [
          [
            '10',
            '20',
            '30',
            '40'
          ],
          [
            '60',
            '70',
            '8',
            '90',
            '10'
          ]
        ];

Then your 'for' loop works:

print join (",", @$_),"\n" for @input_list ;

Because then, @input_list is actually 2 items - two array references that you can then dereference and join.

As a slight word of warning though - one of the gotchas that can occur when doing:

my @input_list = (\@x, \@y);

Because you're inserting references to @x and @y - if you reuse either of these, then you'll change the content of @input_list - which is why it's probably better to use the my @input_list = ( $x_ref, $y_ref );.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent addition! It seems to solve my bug Can't use string ("22.875451") as an ARRAY ref while "strict refs" in use at /Library/Perl/5.18/Math/Geometry/Planar.pm line 622.
I got now this error /Users/code/data.txt : Can't use string ("2") as an ARRAY ref while "strict refs" in use at /Library/Perl/5.18/Math/Geometry/Planar.pm line 622. Is this related to the warning which you added?
No it'll be the same problem - the code is expecting an array reference and it's getting a single value ("2"). Likely root cause is again - a single dimensional array being passed instead of a multi-dimensional one.
4
my @x = qw/10 20 30 40/;
my @y = qw/60 70 8 90 10/;
my @input_list = (@x, @y);

This is equivalent to:

my @input_list = qw/10 20 30 40 60 70 8 90 10/;

To create an array of arrays, use references:

my @input_list = (\@x, \@y);

The rest of your code then works as expected.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.