Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a problem: I am reading file version.txt and adding some format to it row by row. version.txt could look like
Matt 100 John 150 Laura 150
The problem I have, is that when I am calling in main program --system 'perl test'-- file, which looks like this belowe, my format_style function works OK. But when I tried to add the code part to main program and call the function in there instead of system call, I am loosing information about arrays "names" and "vers" in the second while statement. Why is that....
format_style(); # FUNCTION FORMAT_STYLE----------------------------------------------- +------ # Final stage where text is replaced to correct format sub format_style { # Function calls to create desc.txt open (VER,"version.txt"); while (defined ($rows = <VER>)) { chop($rows); ($rows1,$rows2)=split(/\ /,$rows); $names[$i]=$rows1; $vers[$i]= $rows2; $i++; } close VER; $i=0; $ch1=":"; $ch2=" "; open (ROWS, "desc5.txt") || die "File not found"; while (defined ($rivi = <ROWS>)) { chomp $rivi; if($rivi =~ /^$names[$i]/) { chomp $rivi; # IN THIS PART I DONT HAVE ARRAYS ANYMORE AVAILABLE # Counting length of words my $L1=length($names[$i]); my $L2=length($vers[$i]); #print "Length for $names[$i] =$L1, and for $vers[$i]=$L2\n" +; my $pad1=(27-($L1)); my $pad2= (6-($L2)); my $pad1_1=(" " x $pad1); my $pad2_2=(" " x $pad2); #chop $vers[$i]; $rivi="\n$names[$i]$pad1_1$ch1$ch2$vers[$i]$pad2_2$ch1\n"; open (E,">>style1.txt"); print E $rivi; close E; $i++; } elsif ($rivi) { chomp $rivi; my $pad3=(" " x 36); open (E,">>style1.txt"); print E "$pad3$rivi\n"; close E; } + } close ROWS; system 'cp style1.txt desc5.txt'; system 'rm style1.txt'; }
BR Hewarn

Replies are listed 'Best First'.
Re: Lost arrays
by robartes (Priest) on Dec 02, 2002 at 13:19 UTC
    Are you sure your arrays are "missing". When I run your code on my machine, it produces output (names and values padded and seperated by ':'). BTW, if all you want to do is add some format to these lines, there are much less convoluted solutions than the one you give above:
    • Use formats
    • Use pack:
      use strict; open INPUT, "<version.txt" or die "Blerch: $!\n"; while (<INPUT>) { # read a line of input my @contents=split /\s+/; # Split on whitespace print join ":", (pack("A10",$contents[0]), # Name, padded to 10 width pack("A5",$contents[1]), # version, padded to 5 width "\n"); } __END__ Nameone :100 : Nametwo :110 : Namethree :120 :
    • CU
      Robartes-

      Even easier is to use printf.

      #! perl -slw use strict; my (@names, @numbers); while (<DATA>) { chomp; my ($name, $number) = split' '; push @names, $name; push @numbers, $number } # right justified printf "%10s : %8d\n", $names[$_], $numbers[$_] for 0 .. $#names; print ''; # left justified printf "%-10s : %-8d\n", $names[$_], $numbers[$_] for 0 .. $#names; print ''; # strings left justified, numbers right justified printf "%-10s : %8d\n", $names[$_], $numbers[$_] for 0 .. $#names; print ''; __DATA__ nameone 101 nametwo 123456 namethree 1234567 C:\test>216893 nameone : 101 nametwo : 123456 namethree : 1234567 nameone : 101 nametwo : 123456 namethree : 1234567 nameone : 101 nametwo : 123456 namethree : 1234567

      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Lost arrays
by rdfield (Priest) on Dec 02, 2002 at 12:38 UTC
    Have you tried defining a scope for these 'lost' variables? Try re-writing the script with 'stict' and 'warnings' in force, i.e. in this case having the scope of each variable defined, and then see where the arrays go missing.

    rdfield