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
In reply to Is it possible to get all tetra words with correct starting position using a better code within a loop? by supriyoch_2008
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |