Hi PerlMonks,
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 try.pl 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?
Here goes the script try.pl:
#!/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;
I have got the following wrong results in cmd:
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.p
+l
line 8.
ABCD ->Starting at pos 1
BCDE ->Starting at pos 2
CDEF ->Starting at pos 2
DEFG ->Starting at pos 2
EFGH
The correct results should look like:
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.