in reply to Printing an array

You do understand that the three @DATA arrays are totally different, do you?

If not, you must check up on the scoping of variables in perl.

You will have noticed that the readdata-subroutine returns the @DATA array as a flattened list, so you could do a print readdata(); to print the data.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: Printing an array
by shemyaza (Novice) on Nov 24, 2003 at 22:38 UTC
    Thanks for your help and suggestions. I have read up on subroutines now and come up with the code below. I think this addresses a lot of the points and appears to work quite well but I don't see all of my data file (prev.txt) on screen. Have I still got something wrong? TIA S.
    #!/usr/bin/perl -w use strict; my(@DATAFILE); @DATAFILE=&readdata(); &testcode1(@DATAFILE); &writedata(@DATAFILE); # # Subroutine for reading prev.txt into an array # sub readdata { open (PH, "prev.txt") || die "Cannot open prev.txt: $!"; my(@DATA)=<PH>; chomp @DATA; close (PH); return(@DATA); } # # Subroutine for writing array into prev.txt # sub writedata { my(@DATA)=@_; open (PH, ">prev.txt") || die "Cannot open prev.txt: $!"; foreach(@DATA) { print PH "$_\n"; } close (PH); } # # Subroutines for various tests on the code # sub testcode1 { print "@_"; }
      I tested your code and it runs without a hitch. The testcode-subroutine nicely prints out a textfile I happened to have on the hard-disk.

      Of course it was a smallish file and that may be where your problem lies: perhaps --I'm just guessing here-- your file is so large that when it is printed on the screen, some buffers overrun and not everything is shown or parts get lost.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law