sorry! $line_count at the end of the while block should be $line_count++
On Fri, Jan 21, 2011 at 10:54 AM, jbiskofski <jbiskof...@gmail.com> wrote: > my $line_count = 1; > my @captured_elements; > > open(FILE, $filename) || die 'cant open file'; > while (<FILE>) { > chomp; > push(@captured_elements, $_) unless ($line_count % 4); > $line_count; > } > close(FILE); > > ------- > > % is the modulus operator, it returns the remainder of a division so ... 6 > % 4 = 2, 7 % 4 = 3, 8 % 4 = 0, > > so when the remainder of the line_count by 4 is 0, push the element into > @captured_elements > > i hope this helps! > > -Jose > > > On Fri, Jan 21, 2011 at 10:43 AM, jet speed <speedj...@googlemail.com>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 >> > >