Understanding split in a while loop
From: Gavin Henry (ghenry_at_perl.me.uk)
Date: 02/28/05
- Next message: Ankur Gupta: "Re: Understanding split in a while loop"
- Previous message: Ricardo SIGNES: "Re: Case-operator"
- Next in thread: Ankur Gupta: "Re: Understanding split in a while loop"
- Reply: Ankur Gupta: "Re: Understanding split in a while loop"
- Reply: Jenda Krynicky: "Re: Understanding split in a while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 28 Feb 2005 12:26:06 -0000 (GMT) To: beginners@perl.org
Hi all,
Just a quickie. I think the answer is that th while loop is going through
the file twice?
--------------
Codes that does what I expect, i.e. prints out the 42 lines of my passwd
file, with the array spaces, due to the array in "", swapped for <was :>
and runs on $_:
#!/usr/bin/perl
use strict;
use warnings;
$" = '<was :>';
open PASSWD, "/etc/passwd"
or die "Eh? ($!)";
while (<PASSWD>) {
my @passwds = split /:/;
print "@passwds";
}
--------------
This now only prints 21 lines?
I am not sure why? It misses the odd ones.
#!/usr/bin/perl
use strict;
use warnings;
$" = '<was :>';
open PASSWD, "/etc/passwd"
or die "Eh? ($!)";
while (<PASSWD>) {
my @passwds = split /:/, <PASSWD>;
print "@passwds";
}
-- Just getting into the best language ever... Fancy a yourname@perl.me.uk? Just ask!!!
- Next message: Ankur Gupta: "Re: Understanding split in a while loop"
- Previous message: Ricardo SIGNES: "Re: Case-operator"
- Next in thread: Ankur Gupta: "Re: Understanding split in a while loop"
- Reply: Ankur Gupta: "Re: Understanding split in a while loop"
- Reply: Jenda Krynicky: "Re: Understanding split in a while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|