On Fri, Jan 28, 2011 at 3:49 PM, Rob Dixon <rob.di...@gmx.com> wrote:

>  On 28/01/2011 15:26, jet speed wrote:
>
>> Hi All,
>>
>> I need help on this one please.
>>
>> I have a input file with the following data. The 7 times 000s i have added
>> in the input file in order to start with 1808 as the eight element. which
>> works ok. am sure there is a better way.
>>
>> now once i captre the 8th element ex: 1808, then 1810 so on in the list, i
>> print the output. which works fine.
>>
>> similarly i need to capture the following 7 list of elements in the file
>> and
>> print it out with certain parameter.
>>
>> e.g the Required output
>> ------------------------------------------
>>  This is 1808 eigth element
>> This is1809
>> This is 180A
>> This is 180B
>> This is 180C
>> Thsi s 180D
>> This is 180E
>> This is 180F
>> This is 1810 eigth element
>> This is 1811
>> This is 1812
>> This is 1813
>> This is 1814
>> This is 1815
>> This is 1816
>> This is 1817
>> input.txt file
>> 0000
>> 0000
>> 0000
>> 0000
>> 0000
>> 0000
>> 0000
>> 1808
>> 1809
>> 180A
>> 180B
>> 180C
>> 180D
>> 180E
>> 180F
>> 1810
>> 1811
>> 1812
>> 1813
>> 1814
>> 1815
>> 1816
>> 1817
>> #!/usr/bin/perl
>> use strict;
>> use warnings;
>> my $filename;
>> my $counter = 0;
>> my $A;
>> ######### FORM META
>> ########################################################
>>
>> # if the counter  4 the insert 3 times 000 into the file for correct start
>> # if the counter  8 then insert 7 times 000 into the file for correct
>> start
>>
>> $filename = "input.txt" ;
>> open (FILE, "<  $filename" ) or die "Could not open $filename: $!";
>> while (<FILE>) {
>>  chomp;
>>  $counter++;
>>  next unless ($counter == 8);
>> $counter=0; # Of course you should not forget to reset your counter ;-)
>>   print "form meta from dev $_\; \n";
>>  }
>>
>>
>> ##############################################################################
>> Any help on this would be much appreciated.
>>
>
> First some comments on your code:
>
> - It is much better to declare variables as late as possible rather
>  than in a block at the start of your progam
>
> - Always use lexical file handles ($fh instead of FH) and the three-
>  parameter form of open. Well done checking the return status and
>  displaying the value of $!
>
> - You can use the built-in variable $. which keeps track of the current
>  record number of your input file
>
> I suggest something like the program below, which uses the modulus
> operator % on the current line number $. to determine whether we are at
> the start of an eight-line block. This way there is no need to add
> padding to the start of the data.
>
> HTH,
>
> Rob
>
>
> use strict;
> use warnings;
>
> my $filename = "input.txt" ;
>
> open my $fh, '<', $filename or die "Could not open $filename: $!";
>
> while (<$fh>) {
>  chomp;
>  if ($. % 8 == 1) {
>    print "$_ eighth element\n";
>  }
>  else {
>    print "  $_\n";
>  }
> }
>
> **OUTPUT**
>
> 1808 eighth element
>
>  1809
>  180A
>  180B
>  180C
>  180D
>  180E
>  180F
> 1810 eighth element
>
>  1811
>  1812
>  1813
>  1814
>  1815
>  1816
>  1817
>



Hi Rob,

Brilliant, you made it look so simple. Thanks again for your help with this

Kind Regards
TM

Reply via email to