Capture nth element of a file

--90e6ba181976e74195049a5df13f
Content-Type: text/plain; charset=ISO-8859-1

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

--90e6ba181976e74195049a5df13f--
jet speed [ Fr, 21 Januar 2011 17:43 ] [ ID #2053664 ]

Re: Capture nth element of a file

--001636c5b447118774049a5e1993
Content-Type: text/plain; charset=ISO-8859-1

my $line_count = 1;
my [at] captured_elements;

open(FILE, $filename) || die 'cant open file';
while (<FILE>) {
chomp;
push( [at] 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
[at] captured_elements

i hope this helps!

-Jose


On Fri, Jan 21, 2011 at 10:43 AM, jet speed <speedjet5 [at] 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
>

--001636c5b447118774049a5e1993--
jbiskofski [ Fr, 21 Januar 2011 17:54 ] [ ID #2053665 ]

Re: Capture nth element of a file

--001636e1ecff10f150049a5e1be8
Content-Type: text/plain; charset=ISO-8859-1

sorry!

$line_count at the end of the while block should be $line_count++

On Fri, Jan 21, 2011 at 10:54 AM, jbiskofski <jbiskofski [at] gmail.com> wrote:

> my $line_count = 1;
> my [at] captured_elements;
>
> open(FILE, $filename) || die 'cant open file';
> while (<FILE>) {
> chomp;
> push( [at] 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
> [at] captured_elements
>
> i hope this helps!
>
> -Jose
>
>
> On Fri, Jan 21, 2011 at 10:43 AM, jet speed <speedjet5 [at] 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
>>
>
>

--001636e1ecff10f150049a5e1be8--
jbiskofski [ Fr, 21 Januar 2011 17:54 ] [ ID #2053666 ]

Re: Capture nth element of a file

--0015175d676abdbab6049a5e24e9
Content-Type: text/plain; charset=UTF-8

On Fri, Jan 21, 2011 at 5:43 PM, jet speed <speedjet5 [at] 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
>

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 [at] {$A}, $_ . "\n";
}

use Data::Dumper;
print Dumper $A;

--0015175d676abdbab6049a5e24e9--
Rob Coops [ Fr, 21 Januar 2011 17:57 ] [ ID #2053667 ]

Re: Capture nth element of a file

--0016361e83821499da049a5e28b5
Content-Type: text/plain; charset=UTF-8

On Fri, Jan 21, 2011 at 5:57 PM, Rob Coops <rcoops [at] gmail.com> wrote:

>
>
> On Fri, Jan 21, 2011 at 5:43 PM, jet speed <speedjet5 [at] 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
>>
>
> 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);
>
$counter=0; # Of course you should not forget to reset your counter ;-)

> push [at] {$A}, $_ . "\n";
> }
>
> use Data::Dumper;
> print Dumper $A;
>

--0016361e83821499da049a5e28b5--
Rob Coops [ Fr, 21 Januar 2011 17:57 ] [ ID #2053668 ]

Re: Capture nth element of a file

On 1/21/11 Fri Jan 21, 2011 8:43 AM, "jet speed"
<speedjet5 [at] googlemail.com> 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: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Fr, 21 Januar 2011 18:18 ] [ ID #2053670 ]

Re: Capture nth element of a file

--00221532c9d4205b97049a5eec1a
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Jan 21, 2011 at 5:35 PM, jet speed <speedjet5 [at] googlemail.com> wrote:

>
>
> On Fri, Jan 21, 2011 at 5:18 PM, Jim Gibson <jimsgibson [at] gmail.com> wrote:
>
>> On 1/21/11 Fri Jan 21, 2011 8:43 AM, "jet speed"
>> <speedjet5 [at] googlemail.com> 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: beginners-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] perl.org
>> http://learn.perl.org/
>>
>>
>>
> Rob, Jim, Jbiskofski, Thanks very much Guys, Appreciate your help with
> this.
>
> Regds
> Js
>

--00221532c9d4205b97049a5eec1a--
jet speed [ Fr, 21 Januar 2011 18:53 ] [ ID #2053671 ]
Perl » gmane.comp.lang.perl.beginners » Capture nth element of a file

Vorheriges Thema: Script to prepend text to beginning of file
Nächstes Thema: SDL beginner question