Help with the last element in an array

Discussion in 'Perl Misc' started by Deepu, Sep 12, 2006.

  1. Deepu

    Deepu Guest

    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.
     
    Deepu, Sep 12, 2006
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Sep 12, 2006
    #2
    1. Advertising

  3. Deepu

    Deepu Guest

    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.
     
    Deepu, Sep 12, 2006
    #3
  4. 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
     
    John W. Krahn, Sep 12, 2006
    #4
  5. Deepu

    Deepu Guest


    > 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
     
    Deepu, Sep 12, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    473
    Ben Bacarisse
    Dec 6, 2006
  2. Johannes Schaub (litb)

    Advancing past the last element of an array

    Johannes Schaub (litb), Dec 27, 2009, in forum: C Programming
    Replies:
    10
    Views:
    644
    Tim Rentsch
    Jan 13, 2010
  3. Josselin
    Replies:
    7
    Views:
    154
    Nicolas Despr├Ęs
    Feb 21, 2007
  4. Len Lawrence

    Last element skip in array mapping

    Len Lawrence, Jun 13, 2009, in forum: Ruby
    Replies:
    2
    Views:
    103
    Len Lawrence
    Jun 13, 2009
  5. .rhavin
    Replies:
    7
    Views:
    129
    Heinrich Mislik
    Oct 8, 2004
Loading...

Share This Page