On Fri, Jan 21, 2011 at 5:43 PM, jet speed <[email protected]> wrote:
> 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.
>
> Regds
> Sj
>
Using a simple counter would do the trick...
#!/usr/bin/perl
use strict;
use warnings;
my $filename;
my $counter = 0;
my $A;
$filename = "input.txt" ;
open (FILE, "< $filename" ) or die "Could not open $filename: $!";
while (<FILE>) {
chomp;
$counter++;
next unless ($counter == 4);
push @{$A}, $_ . "\n";
}
use Data::Dumper;
print Dumper $A;