Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

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

Hello supriyoch_2008.

As toolic has already informed you, push will allow you to insert the value of $newlist into @newarray on each pass through your loop.

On each iteration, $newlist changes, so because @newarray = "$newlist" is declared outside the scope of this loop, you are essentially creating a single element list and populating it with the last value of $newlist.

I would recommend that you take a look at Perlintro (safety net) and begin incorporating the use strict pragma into your code. This will help you keep cleaner and more maintainable code as well as help to keep you more aware of variable scope.

Perlstyle talks more about that white space that toolic recommended, along with other suggestions that will help you to make your code more readable. Lastly, you might consider having a read through perldebtut, and begin getting comfortable with debugging. As a rookie myself, I can say that using the debugger to walk through your code a step at a time can have a strong impact on your understanding of what is really happening inside your code.

Here's an example implementing use strict;, incorporating push, and eliminating some single use variables that were in the original.

#!/usr/bin/perl use warnings; use strict; use 5.12.4; my $word="ABCDEFGHIJKLMNO"; ## Determining the number of 4-letter Fragments: my $frag_Num = int (length($word)/4); say "Total Fragments= $frag_Num\n"; say "The 4-Letter Fragments are:\n"; my @newarray; my $frag = 0; while ($word =~ /(.{4}?)/igs) { $frag++; my $pos=$-[0]+1; my $leng=length($&); print "Fragment Number: $frag; Fragment: $&\n"; print "Length: $leng; Starting Position: $pos\n\n"; my $newlist=$leng; push @newarray, $newlist; } print"\n Newarray (should be 4 4 4): @newarray\n\n"; exit;


In reply to Re: Is it possible to get the array with all the elements at the end of a loop? by marquezc329
in thread Is it possible to get the array with all the elements at the end of a loop? by supriyoch_2008

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (9)
    As of 2014-08-20 05:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (105 votes), past polls