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

Re: a few basic questions

by IraTarball (Monk)
on Aug 23, 2001 at 20:41 UTC ( [id://107392]=note: print w/replies, xml ) Need Help??


in reply to a few basic questions

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://107392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found