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

array print outside loop

by shivakumar.balur (Initiate)
on Apr 20, 2010 at 08:23 UTC ( #835716=perlquestion: print w/replies, xml ) Need Help??
shivakumar.balur has asked for the wisdom of the Perl Monks concerning the following question:

open FILE, "Params.txt" or die "unable to open\n"; for($i=1,$j=1; <FILE>; $i++, $j++) { chomp; (@paramsi, @charamj) = split(':'); @actuali=@charamj; print "inside @actuali\n"; } #print "out side @actual\n"; close(FILE); Params.txt contains: name:abc label:def Problem: Unable to print "@actual" array outside loop. Please help me in this, m new to perl.

Replies are listed 'Best First'.
Re: array print outside loop
by kyle (Abbot) on Apr 20, 2010 at 12:05 UTC

    I might write your code this way (but note I haven't tested it).

    use strict; use warnings; my $param_file = 'Params.txt'; open my $param_fh, '<', $param_file or die "Can't read '$param_file': $!"; my (@params, @charam); while (<$param_fh>) { my ($name,$value) = split /:/; push @params, $name; push @charam, $value; } my @actual = @charam; close $param_fh or die "Close failed: $!";

    Some differences from your version:

    • I Use strict and warnings (as Ratazong has already suggested).
    • I use three arg open for a little more safety.
    • When open fails, my error message includes $! so I know why, and it also says everything I'd want to know about the open that failed (what file I was opening and what mode I was trying to use—read).
    • The first argument to split is a regular expression.
    • Your $i and $j always have the same value, and you don't really need them since you're just appending to arrays. So I used push instead. That also means the first defined element of my array is 0 instead of 1.

    The data you're reading would probably be best represented as a hash rather than a pair of arrays, but I say that without really knowing what you're going to do with it. If I were sticking all this in a hash, the code would look like this:

    # same preamble my %value_of; while (<$param_fh>) { my ($name,$value) = split /:/; $value_of{$name} = $value; } # etc

    Then you could get the stuff you have in @params and @charam using keys and values, but they'd be out of order.

    If you're not familiar with hashes, I'd suggest you look into it. They are very useful.

    I hope this helps.

Re: array print outside loop
by Ratazong (Monsignor) on Apr 20, 2010 at 08:28 UTC

    Now that is an easy one ;-)

    Your variable is called @actuali, but you want to print a variable called @actual outside the loop.


    PS.: please use code-tags for formatting your code!

    OOps ... I got confused by the [] not shown due to missing code-tags :-(

    Now here is some code (yours, however modified) with comments - and it works :-)

    use strict; # use this always. + no excuses. use warnings; # same with this. my @params; my @charam; my @actual; # declare all your + variables!! my $i; my $j; # declare all your + variables!! open FILE, "params.txt" or die "unable to open\n"; for($i=1,$j=1; <FILE>; $i++, $j++) { chomp; (@params[$i], @charam[$j]) = split(':'); # use $i and $j here, +not i and j!!! $actual[$i]=$charam[$j]; # better use $actual[$ +i] instead of @actual[$i] (same on line above!) print "inside $actual[$i]\n"; } print "out side @actual\n"; # it works, but gi +ves an error as $actual[0] is undefined close(FILE);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://835716]
Approved by rovf
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-06-25 01:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.