help to capture the elements, every 8th, followed by 7 elements

--0023547c9215dd6bd6049ae9af28
Content-Type: text/plain; charset=ISO-8859-1

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.

Regards
TM

--0023547c9215dd6bd6049ae9af28--
jet speed [ Fr, 28 Januar 2011 16:26 ] [ ID #2054035 ]

Re: help to capture the elements, every 8th, followed by 7 elements

On 11-01-28 10:26 AM, jet speed wrote:
> Any help on this would be much appreciated.

See `perldoc perlop` and search for /Multiplicative Operators/. Note
the binary operator %


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Fr, 28 Januar 2011 16:38 ] [ ID #2054036 ]

Re: help to capture the elements, every 8th, followed by 7 elements

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

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 28 Januar 2011 16:49 ] [ ID #2054037 ]

Re: help to capture the elements, every 8th, followed by 7 elements

--0023547c8989aea63e049aea6918
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Jan 28, 2011 at 3:49 PM, Rob Dixon <rob.dixon [at] 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

--0023547c8989aea63e049aea6918--
jet speed [ Fr, 28 Januar 2011 17:18 ] [ ID #2054038 ]
Perl » gmane.comp.lang.perl.beginners » help to capture the elements, every 8th, followed by 7 elements

Vorheriges Thema: about utf8
Nächstes Thema: Redeclaration of variable [different scope]