Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with the last element in an array

Reply
Thread Tools

Help with the last element in an array

 
 
Deepu
Guest
Posts: n/a
 
      09-12-2006
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.

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-12-2006
Deepu wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Deepu
Guest
Posts: n/a
 
      09-12-2006
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.

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-12-2006
Deepu wrote:
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Deepu
Guest
Posts: n/a
 
      09-12-2006

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


I am extremely sorry, actually the code works fine. It was my mistake.
Thanks for the help.

- Deepu

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Advancing past the last element of an array Johannes Schaub (litb) C Programming 10 01-13-2010 08:49 AM
Last element skip in array mapping Len Lawrence Ruby 2 06-13-2009 05:18 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
get rid of last element in an array Josselin Ruby 7 02-21-2007 12:03 PM
one past the last element of an array object junky_fellow@yahoo.co.in C Programming 5 12-06-2006 02:31 PM



Advertisments