On 1/21/11 Fri Jan 21, 2011 8:43 AM, "jet speed"
<[email protected]> scribbled:
> Hi All,
> I need some help with the blow.
>
> I have a an input file with the below data
>
> 1835
> 1836
> 1837
> 1838
> 1839
> 183A
> 183B
> 183C
> 183D
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $filename;
> $filename = "input.txt" ;
> open (FILE, "< $filename" ) or die "Could not open $filename: $!";
> while (<FILE>) {
> chomp;
> print "$_ \n";
> }
>
> I can successfully read the file with this, what i want to achive is to
> capture every 4 th element of the input file into a variable ex: $A.
> that would be
> 1838
> 183C
>
> Any help would be much appreciated.
Perl has a built-in line counter: $. Therefore, you can use the expression
(($. % 4) == 0) to test for every fourth line:
$A = $_ if (($. % 4) == 0)
You can shorten this by using the fact that zero is logical false and
inverting the test:
$A = $_ unless($. % 4);
See 'perldoc perlvar' for this and other built-in variables.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/