Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

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

Hi Perl Monks,

It might be a silly question but I am at my wit's end to get the desired result in the last array (line 19). I have written a perl script (given below) to count the number of 4-letter fragments in a 15-letter word. My interest is to assign the length of each fragment as an element in the final array i.e. @newarray. The new array at line 19 should show the elements (4 4 4) but I am getting only one 4. Is it possible to get the correct result from the newarray as (4, 4, 4)?

Here goes the perl script:

#!/usr/bin/perl use warnings; $word="ABCDEFGHIJKLMNO"; ## Determining the number of 4-letter Fragments: $a=length($word);$four=4; # Line 5 $frag_Num1=$a/$four;$frag_Num=int$frag_Num1; print"\n Total Fragments= $frag_Num\n\n"; print"\n\n The 4-Letter Fragments are:\n\n"; $frag=0; while ($word=~ /(.{4}?)/igs) # Line 10 {$frag++; $b=$&;$pos=$-[0]+1; $leng=length($b); print"\n Fragment: $&; Fragment Number: $frag of length $leng letters + Starting at position: $pos\n"; print"\n Its length= $leng Letters;\n\n"; # Line 15 $newlist=$leng; } @newarray="$newlist"; print"\n Newarray (should be 4 4 4): @newarray\n\n"; # Line 19 exit;

The cmd has shown the correct results except for the last line:

Total Fragments= 3 The 4-Letter Fragments are: Fragment: ABCD; Fragment Number: 1 of length 4 letters Starting at po +sition: 1 Its length= 4 Letters; Fragment: EFGH; Fragment Number: 2 of length 4 letters Starting at po +sition: 5 Its length= 4 Letters; Fragment: IJKL; Fragment Number: 3 of length 4 letters Starting at po +sition: 9 Its length= 4 Letters; Newarray (should be 4 4 4): 4

The last line should look like:

Newarray (should be 4 4 4): (4,4,4)

In reply to Is it possible to get the array with all the elements at the end of 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
    [usemodperl]: greetings
    [usemodperl]: question
    [usemodperl]: perldoc -v 'ARGV' works but perldoc -v "STDIN' does not, but they are both filehandles. is there a perldoc for STDIN?
    [usemodperl]: (typo not withstanding :)
    usemodperl thinks about loose coupling...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2018-06-19 19:17 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (114 votes). Check out past polls.