Help with the last element in an array

D

Deepu

Hi All,

I have a file like: (test1)

SA blk: 2 pg: 4 col: 5 time: 4
SB blk: 4 pg: 4 col: 6 time: 4
SC blk: 5 pg: 3 col: 3 time: 2
SD blk: 1 pg: 2 col: 8 time: 9
SE blk: 8 pg: 0 col: 3 time: 10
SF blk: 2 pg: 1 col: 6 time: 11
SG blk: 5 pg: 9 col: 0 time: 45

I am trying to generate the output:

SA - SB - SC
SB - SC - SD
SC - SD - SE
SD - SE - SF
SE - SF - SG

But i am getting:

SA - SB - SC
SB - SC - SD
SC - SD - SE
SD - SE - SF

Here i am missing out the last SE - SF - SG.

The array in the code below (@seen) has SE - SF and SF - SG. I need to
combine these and delete SF to get SE - SF - SG. Can somebody please
give me some ideas on it.

The code i am using is:

#!/usr/bin/perl

open (FILE, "test1") || die;

while (<FILE>) {
chomp;
push (@array, $_);
#print "@array\n";
}
$elements = $#array;

for my $i (0 .. $elements) {
my $line = $array[$i];
#print $line;

if (($line !~
/^\s*(\S+)\t+blk:\s+(\S+)\s+pg:\s+(\S+)\s+col:\s+(\S+)\s+time:\s+(\S+)/)){
print "Bad line:$line\n";
}

if (@prev) {
my $stKey = "$prev[1]-$1";
#print "######$stKey######\n";
threeState($stKey);
}
@prev = ($line, $1);

}
close (FILE);

my @seen =();

sub threeState($)
{
my $state = $_[0];
print "$_[0]\n";

if (@seen < 2) {
push (@seen, $state);
print "#!#!#!#!#!!#!#!@seen#!#!#!#!#!#!\n";
} else {
my $key=join '-',@seen;
@test = split(/-/,$key);
splice (@test,1,1);
my $key=join '-',@test;
print
"########################################################$key\n";
if ($covValidAA{"$key"}) {
$covSTCntAA{"$key"}++;
#print "$stKey $covSTCntAA{$stKey}\n";
} else {
if (~$covValidAA{"$key"}) {
$covSTCntAA{"$key"}--;
}
}
shift @seen;
push (@seen,$state);
print "@seen\n";
}

}


Thanks for your time.
 
J

John W. Krahn

Deepu said:
I have a file like: (test1)

SA blk: 2 pg: 4 col: 5 time: 4
SB blk: 4 pg: 4 col: 6 time: 4
SC blk: 5 pg: 3 col: 3 time: 2
SD blk: 1 pg: 2 col: 8 time: 9
SE blk: 8 pg: 0 col: 3 time: 10
SF blk: 2 pg: 1 col: 6 time: 11
SG blk: 5 pg: 9 col: 0 time: 45

I am trying to generate the output:

SA - SB - SC
SB - SC - SD
SC - SD - SE
SD - SE - SF
SE - SF - SG

But i am getting:

SA - SB - SC
SB - SC - SD
SC - SD - SE
SD - SE - SF

Here i am missing out the last SE - SF - SG.

The array in the code below (@seen) has SE - SF and SF - SG. I need to
combine these and delete SF to get SE - SF - SG. Can somebody please
give me some ideas on it.

You probably want something like:

#!/usr/bin/perl
use warnings;
use strict;

open FILE, '<', 'test1' die "Cannot open 'test1' $!";

my @array;
while ( <FILE> ) {
print "Bad line:$_" unless
/^\s*(\S+)\t+blk:\s+\S+\s+pg:\s+\S+\s+col:\s+\S+\s+time:\s+\S+/;
push @array, $1;
}
close FILE;

while ( @array >= 4 ) {
print join( ' - ', @array[ 0 .. 3 ] ), "\n";
shift @array;
}

__END__



John
 
D

Deepu

Hi John,

Thanks for the help. I tried the code specified by you but now i
missing out the first transition SA - SB - SC. How can this be
corrected.


#!/usr/bin/perl
use warnings;
use strict;

open FILE, '<', 'test1' die "Cannot open 'test1' $!";

my @array;
while ( <FILE> ) {
print "Bad line:$_" unless
/^\s*(\S+)\t+blk:\s+\S+\s+pg:\s+\S+\s+col:\s+\S+\s+time:\s+\S+/;
push @array, $1;
}
close FILE;

while ( @array >= 4 ) {
print join( ' - ', @array[ 0 .. 3 ] ), "\n";
shift @array;
}

__END__


Thanks for the help.
 
J

John W. Krahn

Deepu said:
Hi John,

Thanks for the help. I tried the code specified by you but now i
missing out the first transition SA - SB - SC. How can this be
corrected.

I don't know what to tell you, it works fine for me.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top