Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^3: Passing Along Arrays

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

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

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.

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

    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.

Re^4: Passing Along Arrays
by Perl_Ally (Novice) on Aug 26, 2014 at 21:36 UTC
    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

        Thanks for the attention and suggestion 2teez

        I realize it would be more concise the declare the variables as they come into use (and I prefer the look of it that way myself) but I work in a professional environment (which is why I'm not at liberty to share the entirety of my code with the world wide web) with coding guidelines and conventions. It is convention to declare all variables at the outset to better document the routine.

        $proj and $pointerProj are string variables defined elsewhere and used in the naming convention of the data files.

      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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2023-03-26 06:24 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (63 votes). Check out past polls.