perlquestion
supriyoch_2008
<p>Hi PerlMonks,</p>
<p>I think it might be a very silly question to ask but I am at my wit's end to find the
solution. I am interested to produce the first tetra-word (ABCD) from a long string (ABCDEFGH), then to delete
the first letter from the string and get the tetra word (BCDE) along with its starting position. I have written
a script <code> try.pl </code> using foreach LOOP which shows the tetra words but starting positions for
tetra-word CDEF and DEFG are wrong. Moreover, the loop does not show the last tetra-word
i.e. EFGH. So, after the loop I have written the code in line 15 to get the last tetra-word.
Can I expect a better code to get all the tetra words i.e. ABCD,BCDE,CDEF,DEFG,EFGH
with correct starting positions?</p>
<p>Here goes the script try.pl:</p>
<code>#!/usr/bin/perl
use warnings;
$pro="ABCDEFGH";
@pro=split('',$pro);
print"\n\n Tetra words are:\n";
$one=1;
foreach my $item (@pro) { @tetra=@pro [0..3];
$pos=$+[0]+$one; # Line 8
$tetra=join('',@tetra);
print"\n $tetra ->Starting at pos $pos\n";
$pro =~ s/.//;
@pro=split('',$pro);
}
# To get the last tetra:
$last=join('',@pro); print"\n $last\n"; # Line 15
exit;</code>
<p>I have got the following wrong results in cmd:</p>
<code>Microsoft Windows [Version 6.1.7600]
C:\Users\x>cd desktop
C:\Users\x\Desktop>try.pl
Tetra words are:
Use of uninitialized value in addition (+) at C:\Users\x\Desktop\try.pl
line 8.
ABCD ->Starting at pos 1
BCDE ->Starting at pos 2
CDEF ->Starting at pos 2
DEFG ->Starting at pos 2
EFGH </code>
<p>The correct results should look like:</p>
<code> Tetra words are:
ABCD ->Starting at pos 1
BCDE ->Starting at pos 2
CDEF ->Starting at pos 3
DEFG ->Starting at pos 4
EFGH ->Starting at pos 5</code>