in reply to Re: Passing Along Arrays
in thread Passing Along Arrays

I tried updating readOnly() to return a reference, and using -> as you indicated, but still no dice - I get the same error message.

I realize its hard to debug without a living program so I appreciate your effort. I cannot share more than I already have.

Previously I had readOnly() returning @contents and could store the result as follows:

my @data = readOnly( "data.txt" );

With readOnly() returning \@contents, how would I then store the result?

my @data = ?

Replies are listed 'Best First'.
Re^3: Passing Along Arrays
by frozenwithjoy (Priest) on Aug 26, 2014 at 21:02 UTC

    Is this an accurate depiction of what you are attempting?

    perl -E ' use strict; use warnings; my @projects; push @projects, [ [ "the", "data" ], "key"]; push @projects, [ [ "more", "data" ], "another key"]; for (@projects) { my @data = @{ $_->[0] }; my $key = $_->[1]; say "DATA: @data"; say "KEY: $key"; } '

    OUTPUT:

    DATA: the data KEY: key DATA: more data KEY: another key

    I suggest you print (or use Data::Dumper/Data::Printer on @contents before it is returned and on $_ in the for loop to see what all is going on.

      Well ultimately I'll be manipulating the data rather than printing it, but yes, you've got the gist of it.

      I know readOnly() is working correctly and @contents has the data I want. These pieces have been working and used for some time, which is why this is so perplexing to me.

      @projects and its for-loop is the new element here.

      Previously I manipulated the data sets one by one, applying the same algorithm to each, one after the other. This was obviously inefficient - I had the same code twice, making updates and maintenance ridiculous.

      The @projects for-loop was my solution.

      Now I'm just struggling to pass the array containing the data correctly, as you've surmised.

      I got it to work as follows:

      Nope, I didn't.

      sub extractData { my @projects; push @projects, [ \readOnly( "$proj Data.txt" ), "A"]; unless( $pointerProj eq "NONE" ) { push @projects, [ \readOnly( "$pointerProj Data.txt", "B"]; } for( @projects ) { my @UTdata = $_->[0]; my $key = $_->[1]; ... } } sub readOnly { my @contents; my $filePath; my $file; my $errMsg; $filePath = "$path\\" . $_[0]; $errMsg = "Unable to read $filePath: $!"; open $file, '<', $filePath or die $errMsg; @contents = <$file>; close $file; return @contents; }

      Thanks for the help. I had been stuck on this for a while so some outside input was really needed.

        There is no close paren on this:
        push @projects, [ \readOnly( "$pointerProj Data.txt", "B"];

        Also, instead of using \readOnly(), what happens if you put it inside its own square brackets like the following?

        push @projects, [ [ readOnly( "$pointerProj Data.txt" ) ] , "B"];

        Hi Perl Ally,

        Some observations and questions:
        In readOnly subroutine, why declare the variable first before assignment? i.e

        my @contents; my $filePath; my $file; my $errMsg;
        From the usage in the subroutine, you can actually use them straight. At least, if not for anything, it shorten the line of codes to read through.
        You really don't have to do explicit close $file; The file should close, when the subroutine returns.
        Then in your extractData subroutine, what does $pointerProj variable stand for. It must be a variable defined somewhere in the code?
        Suggestion (troubleshooting): Why not print out, what you have in "@projects" before the for-loop, to be sure you are getting desired data first?
        NOTE: Since one can't have more information than already provided. One can on speculate.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
        I've had success as follows:
        sub extractData { my @projects; push @projects, [ readOnly( "$proj Data.txt" ), "A" ]; unless( $pointerProj eq "NONE" ) { push @projects, [ readOnly( "$pointerProj Data.txt" ), "B" ]; } for( @projects ) { my @data = @{ $_->[0] }; my $key = $_->[1]; ... } }

        Where readOnly() returns \@contents

        Thanks again for the help frozenwithjoy, 2teez