Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to grab every 5th row of an array? 1

Status
Not open for further replies.

chep80

Technical User
Jul 22, 2005
10
US
Here is a portion of my perl script:

my $input = 'C:\Documents and Settings\CheplakM\My Documents\Stk 6.0\STK scripts\anad1_matt.txt';
my @output;
open (FILE, "$input");
my @array = <FILE>;
for (@array) {
my ($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8) = split(/\s+/, $_);
my $line = "$col1 $col4 $col5 $col6 $col7";
if ($line=~m/^\d/) {
push (@output, $line);
}
}

Currently I'm extracting certain columns from the original matrix (col1, and col4 through 7). I now need to extract certain rows. I want the first row, the sixth row, then the eleventh row, etc. There are about 150,000 rows. I need to know what statement is required to extract the first row followed by an increment of 5. I'm assuming this statement would come right after the statement "my $line = "$col1 $col4 $col5 $col6 $col7";

Thanks
 
Code:
my @array = <FILE>;
$trip=0;
for (@array) {
    my ($col1, $col2, $col3, $col4, $col5, $col6, $col7, $col8) = split(/\s+/, $_);
    my $line = "$col1 $col4 $col5 $col6 $col7";
    if (($line=~m/^\d/) [COLOR=red]&& ($trip==0)[/color] {
        push (@output, $line);
    }
    [COLOR=red]$trip++;
    if ($trip == 5) {
      $trip=0;
    }[/color]
}
NOT tested
But it should get you on the right road

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
then you need to using indexing and use an array slice to get only the columns you want from each row:

Code:
my $input = 'C:\Documents and Settings\CheplakM\My Documents\Stk 6.0\STK scripts\anad1_matt.txt';
my @output;
open (FILE, "$input") [b] or die "$!";[/b]
my @array = <FILE>;
close(FILE);
for [b](my $i = 0; $i <= @array+5; $i+=5)[/b] {
    my ($col1, $col4, $col5, $col6, $col7) = (split(/\s+/, [b]$array[$i][/b]))[0,3,4,5,6];
    my $line = "$col1 $col4 $col5 $col6 $col7";
    if ($line=~m/^\d/) {
        push (@output, $line);
    }
}
 
When I use the variable "$trip", I get the error: "$trip requires explicit package name." Can't figure out why its not working. Thanks,
 
need to declare it with my before using it, eg

my $trip;
 
if you use the "trip" method you will need to go through each line of the file/array, run the regexp to see if it matches on all 150,000 lines and check the trip count. If you use indexing like I showed you, you will only process the lines you want, every 5th line. So you will only process 30,000 lines instead of 150,000.
 
i know this is a Perl forum... but do you have access to awk?

Code:
awk 'NR % 5 == 0 { print $1 " - " $6 " - " $11 }' 150000.txt


Kind Regards
Duncan
 
hehehe ;-)

oooh - i feel a ruby solution brewing up... NOT!


Kind Regards
Duncan
 
is this a bit simpler? no array required

Code:
[b]#!/usr/bin/perl[/b]

open (FILE, 'C:\Documents and Settings\CheplakM\My Documents\Stk 6.0\STK scripts\anad1_matt.txt');

while (<FILE>) {
    @cols = split(/\s+/, $_);
    push (@output, "$col0 $col3 $col4 $col5 $col6") if ++$counter % 5 == 0;
}


Kind Regards
Duncan
 
but it seems to me you are still processing every line of the file unnecessarily doing it that way, even if the amount of lines of code is less, it looks like it's doing more work.
 
Hi Kevin

Yes - i appreciate that it is

However, it is not needing to load the entire data set of 150,000 rows and several columns into an array...

It takes less than 2 seconds to run on my machine - processing every line - and printing it to the screen

However, your point is quite valid


Kind Regards
Duncan
 
this might be a compromise:-

Code:
[b]#!/usr/bin/perl[/b]

open (FILE, 'C:\Documents and Settings\CheplakM\My Documents\Stk 6.0\STK scripts\anad1_matt.txt');

while (<FILE>) {
  if ( ++$counter % 5 == 0 ) {
    @cols = split(/\s+/, $_);
    push (@output, "$col0 $col3 $col4 $col5 $col6");
  }
}

at least it doesn't have to do much if it is NOT divisible by 5!


Kind Regards
Duncan
 
yea, it's probably a good trade off, skipping the array but processes all the lines.
 
Progress is made, didn't expect this one to get so busy ;-)

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I continue to get the following error as if it doesnt recognize the variable $counter, even though I initialize it to zero before the loop:

"$counter" requires explicit package name
 
that menas it needs to be declared with 'my' before you use it within it's intended scope.:

Code:
my $counter = 0;
while (<FILE>) {
  if ( ++$counter % 5 == 0 ) {
    @cols = split(/\s+/, $_);
    push (@output, "$col0 $col3 $col4 $col5 $col6");
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top