Well,
1) is easy. Use the modulo operator %
It returns the remainder after a division so $number % 2 is only zero for even numbers and is 1 for odd numbers. See the perlop page for lots of info.
2) is a bit harder. I'm sure there are many elegant solutions. Some probably got posted while I was looking into it. My first try, if the files aren't too big, would be to slurp them into arrays and then fold them together.
use strict;
use warnings;
open (T1, "t1") or die;
my @file1 = <T1>;
my @file2 = <DATA>;
my @file;
while (@file1 and @file2) {
push @file, (shift @file1, shift @file2);
}
push @file, @file1 or @file2; #catch any leftover
while (my $line = shift @file) {
#do 'things' with it
print $line;
}
__END__
file1
file1
file1
file1
the t1 file looks like the END block but with 2 instead of 1
I get
Useless use of private array in void context at junk line 13.
file2
file1
file2
file1
file2
file1
file2
file1
file2
file3, kidding... file2
Not sure what the warnings for, I guess because one of the array's is empty maybe? I'll bet if you put that into an if/else it wouldn't complain.
There's probably a more elegant solution, but this is what comes to mind.
3) Did you try it? I just ran the code you posted (cleaned up with ';' :-) and got this
use strict;
use warnings;
$/ = "****";
my @array;
while (<DATA>) {
chomp;
push @array, $_;
$/= "####";
}
print "@array";
__END__
stuff****more
stuff####less
and got
$ perl junk
stuff more
stuff less
Hope this helps
Ira
"So... What do all these little arrows mean?"
~unknown |