### Is it possible to get the array with all the elements at the end of a loop?

by supriyoch_2008 (Monk)
 on Oct 29, 2012 at 15:44 UTC Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

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)

Replies are listed 'Best First'.
Re: Is it possible to get the array with all the elements at the end of a loop?
by toolic (Bishop) on Oct 29, 2012 at 15:52 UTC
I think you want push:
```#!/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 "Total Fragments= \$frag_Num\n\n";
print "The 4-Letter Fragments are:\n\n";
\$frag = 0;

while ( \$word =~ /(.{4}?)/igs )    # Line 10
{
\$frag++;
\$b    = \$&;
\$pos  = \$-[0] + 1;
\$leng = length(\$b);
print "Fragment: \$&; Fragment Number: \$frag of length \$leng letter
+s Starting at position: \$pos\n";
print "Its length= \$leng Letters;\n\n";    # Line 15
\$newlist = \$leng;
push @newarray, \$newlist;
}
print "\n Newarray (should be 4 4 4): @newarray\n\n";    # Line 19
exit;

__END__

Last line of output is now...

Newarray (should be 4 4 4): 4 4 4
• I ran your code through perltidy (a little whitespace is a good thing).

Toolic,

Thanks for the code. Yes, the perl code push as shown by you has served my purpose.

Regards,

Re: Is it possible to get the array with all the elements at the end of a loop?
by marquezc329 (Scribe) on Oct 30, 2012 at 01:06 UTC

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;

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001385]
Approved by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-02-19 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (258 votes). Check out past polls.

Notices?