1

I'm trying to create an array-of-arrays with some data in a for loop. The regex command in the code below helps me to gather the scalars that I will place in it. As far as I know, that is correct, but when I try to output the @output array to a CSV file I receive a "Can't use string () as an ARRAY ref while "strict refs" in use." error. Is this because of the way I am creating the array or the way I'm trying to write it into a file?

foreach my $row(@input){
    my @cmd = qx("command");
    foreach my $line(@cmd){
        if($line =~ /regex/){
            push(@output, ($sp_name, $sp_port, $sp_type, $sp_uid)); 
        }
    }
}

The code below is what I am using to create my output file::

my $csv = Text::CSV->new()
    or die "Cannot use Text::CSV ($!)";
my $file = "output.csv";
open my $fh, '>', $file
    or die "Cannot open $file ($!)";
$csv->eol("\n");
foreach my $row (@output)
{
    $csv->print($fh, \@{$row})
        or die "Failed to write $file ($!)";
}
close $fh
    or die "Failed to close $file ($!)";
1
  • 1
    After you apply the fixed mentioned below, change \@{$row} back to the simpler $row. Commented Nov 7, 2013 at 21:46

1 Answer 1

5

This is pushing four scalars onto @output:

push(@output, ($sp_name, $sp_port, $sp_type, $sp_uid)); 

The parentheses do nothing but uselessly control precedence. Use square brackets:

push @output, [ $sp_name, $sp_port, $sp_type, $sp_uid ];

The square brackets create an array and return a reference to it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.