Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Passing Along Arrays

by frozenwithjoy (Priest)
on Aug 26, 2014 at 20:35 UTC ( #1098668=note: print w/replies, xml ) Need Help??

in reply to Passing Along Arrays

It's hard to test without having code I can run, but I changed a couple lines (marked w/ comments in the code and described more completely below) to get the functionality I think you want. Does the following work how you want it to?
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] }; # WAS: my @data = @{ $_[0] }; my $key = $_->[1]; # WAS: 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; # WAS: return \@contents; }

I changed readOnly() to return an array reference. Each project is then an array reference with another array reference (the 'data') at index 0 and a text string (the 'key') at index 1. To get these two items, you need to dereference the outer array reference with $_->[X]. You used $_[X] which is how you get an item from an array, not an array reference.

Replies are listed 'Best First'.
Re^2: Passing Along Arrays
by Perl_Ally (Novice) on Aug 26, 2014 at 20:54 UTC

    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 = ?

      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"; } '


      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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098668]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-03-22 19:05 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.