Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 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

#!/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> 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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (12)
    As of 2018-05-24 14:32 GMT
    Find Nodes?
      Voting Booth?