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
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 "@_";
}
| [reply] [d/l] |
|
|
| [reply] [d/l] |