0

I am using below commands to find the size of array

$size_x = @x;

or

$size_x = $#x+1 ;

Both work well when I use in simple statement. But when I use them in the loop, the size becomes one number bigger. Why it is happening in that way. Below is example:

for ($i=1;$i<=10;$i++){

if (1**2+2**2>=1){
@x[$i] =2+3;
$size_x = @x;
}
print "my size is $size_x\n";
}

here is results:

my size is 2
my size is 3
my size is 4
my size is 5
my size is 6
my size is 7
my size is 8
my size is 9
my size is 10
my size is 11

The answer should be from 1 to 10 instead of 2 to 11, i think. What is a better way to get size correctly? Thanks.

mee mee
  • 547
  • 3
  • 8
  • 20

4 Answers4

2

After reading your code, I honestly can't figure out what you're trying to do here, but if you're trying to create an array with 11 elements and assign all of them to 5 except for the first one, then you've done an excellent job. Perhaps it would help to see a visualization of the array you've created:

[undef, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

If that's really what you were hoping for, there are faster/simpler/better ways to do the same thing, but I have to believe you were trying to accomplish something else. At a very basic level, arrays in Perl are 0-indexed:

Normal arrays are ordered lists of scalars indexed by number, starting with 0.

Having said that, you very rarely see the C-style for loop in Perl code, and that's because it's very rarely necessary. More often, you would see something like this:

for (0 .. 9) {
    # do something...
}

That code uses a foreach-style loop and the range operator (..) to generate a sequence (technically, a list of values from left to right, counting up by one). Enough about loops. Let's talk about your strange logic.

12 + 22 will always be 5, which will always be greater than 1. The same goes for 2 + 3. There's nothing dynamic about this code, which is why I have to think that you meant to use the loop iterator in your calculations somehow. Otherwise, you could easily fill a 10-element array with a bunch of 5's in one line:

my @x = (5) x 10;

Now, one last point that applies to this code and all Perl code you write from this point forward. Every single file must include the following two lines at the top:

use strict;
use warnings;
Community
  • 1
  • 1
Matt Jacob
  • 6,503
  • 2
  • 24
  • 27
1

I believe that Perl arrays are zero-based, meaning that the first element is stored and accessed at index 0. Here is your for loop:

for ($i=1; $i<=10; $i++) {
    if (1**2+2**2>=1) {
        @x[$i] =2+3;
        $size_x = @x;
    }
    print "my size is $size_x\n";
}

When you make the first assignment @x[1] = 2 + 3, the array is considered to already have an (empty) element in position zero, so the size is returned as being 2.

The easiest way to avoid this problem from happening would be to refactor your for loop to start at index 0:

for ($i=0; $i<10; $i++) {
    ...
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

When you start the index from 1 the size automatically increases by 1. Try starting the index from 0 or

@x[$i-1] =2+3;
sudheesh shetty
  • 358
  • 4
  • 14
-1

Just as a side note: Array elements in Perl are denoted with a starting $, not an @. So @x is the whole array, while $x[$i] is the $i'th element of the array @x.

use warnings; should warn about this.

(I know this is more a comment than an answer, but as a SO newbie I'm not yet allowed to comment. Sorry.)

PerlDuck
  • 5,610
  • 3
  • 20
  • 39
  • Technically true, although not invalid, because it's basically an array slice that only slices one element. (Also not the root cause of the problem, so it doesn't do a good job of answering the question, as you noted.) – Matt Jacob Jan 30 '16 at 05:19