Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: Passing Along Arrays

by Perl_Ally (Novice)
on Aug 26, 2014 at 21:36 UTC ( [id://1098682]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: Passing Along Arrays
by frozenwithjoy (Priest) on Aug 26, 2014 at 22:13 UTC
    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"];
Re^5: Passing Along Arrays
by 2teez (Vicar) on Aug 26, 2014 at 21:52 UTC

    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.

Re^5: Passing Along Arrays
by Perl_Ally (Novice) on Aug 27, 2014 at 13:40 UTC
    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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found