jkmiller has asked for the wisdom of the Perl Monks concerning the following question:

My First "post"!

I'm getting unexpected (INCORRECT??!!)
results from the "index" function.

I'm looking for enlightenment.

My code and output follow:

#!/usr/bin/perl -w open (IN, "one.seq.one.line"); open (OUT, ">out.index.pl"); while(<IN>){ print OUT $_; print OUT "The first occurance of little a is: ", index($_,"a"), "\n"; print OUT "The first occurance of little c is: ", index($_,"c"), "\n"; print OUT "The first occurance of little g is: ", index($_,"g"), "\n"; print OUT "The first occurance of little t is: ", index($_,"t"), "\n"; print OUT "\n\n"; }
Here is the output

ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagttccaggg +ctgatgaggg The first occurance of little a is: 48 The first occurance of little c is: 47 The first occurance of little g is: 46 The first occurance of little t is: 49
Note that the last line does NOT give the position of
the first little t (should be 55).
Also note that if I change the order in the code so
that the I put the index($_,"t") line first
(before the other three), then it gives the correct
results.

Thanks much!
Would anyone be willing to use my exact code and
put the above line (starting with ATG...) into
the file "one.seq.one.line", and see if they get
the same results as I did???

I'm an admitted newbie, but my systems administrator
looked at if for about 15 mins this morn and thought
it was funny as well.

Thanks much!!

Replies are listed 'Best First'.
Re: Funny results of "index"
by Zaxo (Archbishop) on Jul 16, 2004 at 22:20 UTC

    It works for me,

    $ perl -e'$_="ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaat +cctgagttccagggctgatgaggg"; print "a", index($_,"a"), $/, "c", index($ +_,"c"), $/, "g", index($_,"g"), $/, "t", index($_,"t"), $/' a48 c47 g46 t55
    Are you doing something more besides in the real code?

    After Compline,
    Zaxo

Re: Funny results of "index"
by eric256 (Parson) on Jul 16, 2004 at 22:19 UTC

    Here is my attempt at reproducing your problem

    $_ = "ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagtt +ccagggctgatgaggg"; print $_ . "\n"; print "The first occurance of little a is: ", index($_,"a"), "\n"; print "The first occurance of little c is: ", index($_,"c"), "\n"; print "The first occurance of little g is: ", index($_,"g"), "\n"; print "The first occurance of little t is: ", index($_,"t"), "\n"; __END__ ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagttccaggg +ctgatgaggg The first occurance of little a is: 48 The first occurance of little c is: 47 The first occurance of little g is: 46 The first occurance of little t is: 55

    As you can see it works perfectly here. Is there something else you might be excluding that is influencing it?


    ___________
    Eric Hodges
Re: Funny results of "index"
by hossman (Prior) on Jul 17, 2004 at 07:01 UTC

    Just for kicks, try changing line the first 3 lines to...

    #!/usr/bin/perl -w open (IN, "one.seq.one.line") or die "can't open IN: $!"; open (OUT, ">out.index.pl") or die "can't open OUT: $!"; print OUT scalar(localtime), "\n";

    ..then check to make sure that when you run the program, it doesn't die, and the date stamp gets written to you output file.

    I suspect your code works fine, but the OUT stream isn't getting opened correctly and you just keep looking at an old version of the output file.

    that's just my guess.

Re: Funny results of "index"
by Anonymous Monk on Jul 16, 2004 at 22:28 UTC

    Which version of Perl are you using?

    IWFM

    #!/usr/bin/perl -w use strict; printf "Perl %vd\n",$^V; while(<DATA>){ print $_; for my $letter ('a' .. 'z') { my $index = index($_,$letter); next if $index < 0; print "The first occurence of '$letter' is: ", $index, "\n"; } print "\n\n"; } __DATA__ ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagttccaggg +ctgatgaggg

    Output:

    Perl 5.6.1
    ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagttccagggctgatgaggg
    The first occurence of 'a' is: 48
    The first occurence of 'c' is: 47
    The first occurence of 'g' is: 46
    The first occurence of 't' is: 55
    
    
    Perl 5.8.4
    ATGGACTGCACCTGGAGGATCCTCTTCTTGGTGGCAGCAGCTACAGgcaagagaatcctgagttccagggctgatgaggg
    The first occurence of 'a' is: 48
    The first occurence of 'c' is: 47
    The first occurence of 'g' is: 46
    The first occurence of 't' is: 55