Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

a few basic questions

by maddfisherman (Sexton)
on Aug 23, 2001 at 20:02 UTC ( #107378=perlquestion: print w/replies, xml ) Need Help??

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

1)is there a way to test if a number is odd or even
2)is there a way to alternate between loops like if you wanted to print 1 line from file a and then 1 line from file b and so on.
3)and lastly what happens if you change $/ during the loop of a <> operator. Example:
file data stuff****more stuff#### end file data code local $/ = "****" while <FILE> { chomp $_ my @array = (push, $_) $/= "####"}
would this result in an array like this:
@array = ("stuff", "morestuff")
or would the <> operator reset and give this result:
@array = ("stuff", "stuff***more\nstuff")
or can you not do this at all and it would give an error message.
THANKS (by the way, hope to have fairly usfull template program that i will release for the monks in a few days once it works perfectly!)

Replies are listed 'Best First'.
Re: a few basic questions
by dragonchild (Archbishop) on Aug 23, 2001 at 20:13 UTC
    1. if ($num % 2) { # This is an odd num
    2. Use the same loop for both files, like:
      foreach (0 .. $#lines) { # print from file A # print from file B }
    3. Have you tried it out?

    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Re: a few basic questions
by maverick (Curate) on Aug 23, 2001 at 20:14 UTC
    1) odd or even
    if ($num % 2 == 0) { #even } else { #odd }

    2) you'll probably have to use the eof() instead of < >

    while(!eof(FILE1) && !eof(FILE2) { my $file1 = <FILE1>; my $file2 = <FILE2>; }
    You'll have to deal with the condition where one file is longer than the other below this while.

    3) if localized it will alter the subsiquent reads for only that pass through the loop.

    my @eol = ("\n","\t","\r"); # etc, etc while(<FILE>) { local $/ = shift @eol; my $line = <FILE>; }
    First line ends with "\n", second line ends with "\t", third ends with "\r", and so on.

    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: a few basic questions
by Hofmator (Curate) on Aug 23, 2001 at 20:14 UTC

    1. Use the modulus operator (perlop).
    2. What about using one loop and putting the statements after each other. Pseudocode:
      open File1 open File2 while (condition) { if (read from File1) { do something } if (read from File2) { do something; } }
    3. Have you tried it? What does it do?

    -- Hofmator

Re: a few basic questions
by ozone (Friar) on Aug 23, 2001 at 20:14 UTC
    can't help with the last two questions (not sure what you mean with the second), but here's the first one:
    my $counter = 0; while(1) { print "Got an even number\n" if(($counter++%2) == 0); }
    This will only print when $counter is even...
Re: a few basic questions
by IraTarball (Monk) on Aug 23, 2001 at 20:41 UTC
    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

    "So... What do all these little arrows mean?"

Re: one line each
by cLive ;-) (Prior) on Aug 24, 2001 at 04:39 UTC
    another timtodi coz I like this cute obfu (courtesy of EPP):
    # store files in arrays open(A,'file_a'); my @a = (<A>); close(A); open(B,'file_b'); my @b= (<B>); close(B); # now the fun obfu bit my $max = [ @b + 0 => @a + 0 ] -> [ @b + 0 <= @a + 0 ]; for (0 .. $max) { print $a[$_] . $b[$_]; }

    cLive ;-)

??? how to print only one line
by maddfisherman (Sexton) on Aug 23, 2001 at 20:24 UTC
    how do you print one line then get out of the loop and go back to it later and print the second line, what i mean is how do print line 1 from file a and then print line 1 of file b and then print line 2 file a and then line2 file b and so on.
      This sounds like a job for the evil 'goto'...
      use strict; use warnings; open (T1, "t1") or die; my $line; my $flag = 1; my $counter; DATA: while (<DATA>) { print; goto T1; } T1: while (<T1>) { print; goto DATA; } __END__ file1 file1 file1 file1
      This prints...
      file1 file2 file1 file2 file1 file2 file1 file2 file2 file3, kidding... file2


      "So... What do all these little arrows mean?"

      In the spirit of TMTOWTDITDUG, here's my idea:

      #!/usr/bin/perl -w use strict; my ($fh1, $fh2); open $fh1, "file1.dat" or die "I no read file 1.\n"; open $fh2, "file2.dat" or die "I no read file 2.\n"; my $mainfh = $fh1; while (<$mainfh>) { print; $mainfh = ($mainfh == $fh1) ? $fh2 : $fh1; }
      I'm sure this
      pretty that works.
      And finally, output:
      I'm pretty sure that this works.

      His Royal Cheeziness

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://107378]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-06-29 04:34 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (94 votes). Check out past polls.