R
Rita
for my $i (0..$#sort_start){
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
Rita said:for my $i (0..$#sort_start){
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
Rita said:for my $i (0..$#sort_start){
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
I agree because In my programm when i write first line it is workingit_says_BALLS_on_your forehead said:almost.
if you do:
for (my $i=0;$i<=scalar(@sort_start);$i++){
^^ means that you will get an array index out of
bounds error, since
scalar(@sort_start) will return the length, or number of elements, in
the array, which is 1 more than the last index, since arrays are
0-based.
other than that i believe they do the same thing, although the 1st one
is faster in Perl.
I did then i asking you No they are not doing same thing .second forPaul said:Rita said:for my $i (0..$#sort_start){
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
Why didn't you just try it yourself?
#!/usr/bin/perl
use strict;
use warnings;
my @sort_start = (1, 2, 3, 4);
for my $i (0..$#sort_start) {
print "$i: $sort_start[$i]\n";
}
for (my $i=0;$i<=scalar(@sort_start);$i++){
print "$i: $sort_start[$i]\n";
}
__END__
Do they do the same thing for you?
Rita wrote: ....
Why didn't you just try it yourself?
it's not like that i just didn't find that for loop so i asked to youI don't see Rita's posts any more, so I only catch glimpses through
others' responses.
I am going to suggest that the problem is not that Rita did not try it
herself. The problem is that she did not even stop to think about what
she wants to do. This is evident in the way she asks the questions. No
one, who thought for one second before firing away would have come up
with that sentence.
Rita said:it's not like that i just didn't find that for loop
so i asked to you is anything wrong in it?
and it's not like that i don't try first i tried then i ask at groups .
is anything wrong to use google groups then tell me i will not
later??????
Rita said:I agree because In my programm when i write first line it is working
properly but in second line it's giving me error in last loop i think
bcoz of array's length.
Rita said:for my $i (0..$#sort_start){
for (my $i=0;$i<=scalar(@sort_start);$i++){
Is mean of this both line is same.
John said:Thanks.
Also, in older versions of Perl the list created by 0..$#sort_start would have
to be stored entirely in memory.
ThanksSherm said:Yes - the array is zero-based. So, an array with N elements has indexes of
0 to N-1. The scalar() function will return N, which is one past the last
valid index in the array.
But this command means loop will be start from 0 and will be end
.....?
and what steps it will br take .
like if i want that this loop
works like
for (my $i=0; $i<=scalar(sort_start);$i+=2){
than how you can write this command in that way?
All right ,ThanksJürgen Exner said:But this command means loop will be start from 0 and will be end
.....?
Wrong way of thinking. (0..$#sort_start) will create a list containing all
numbers between 0 and the last index in the array.
You could just as well create a list manually like (1,4,2,6,2,8,7).
Then the loop will simply iterate over this list, setting $i to each value
in turn.
Note: in case of the normal (0..something) the actual implementation is more
optimized, but that is of no concern here.
and what steps it will br take .
No idea what you mean with this sentence
like if i want that this loop
works like
for (my $i=0; $i<=scalar(sort_start);$i+=2){
than how you can write this command in that way?
You want only half the numbers of iterations? Then make the list half as
long: (0..$#sort_start/2)
Of course you will also have to adjust the indexes, i.e. instead of
$array[$i] you have to use $array[$i*2].
jue
Sherm said:You can reduce the speed penalty by calling scalar() once and storing the
result, instead of calling it with every loop repetition:
my $sizeof_sort_start = scalar(@sort_start);
for (my $i=0; $i < $sizeof_sort_start; $i++) {
Joe Smith said:But the scalar operator doesn't return the array size by
iterating over all the values;
Both methods should operate at the same speed since they both
look up a variable in the symbol table, then return an int
stored in the struct that describes the variable.
Rita said:But this command means loop will be start from 0 and will be end .....?
and what steps it will br take
.like if i want that this loop works
like
for (my $i=0; $i<=scalar(sort_start);$i+=2){
than how you can write this command in that way?
Thanks,yes i don't know that $#a means length of array.Brad said:Rita said:But this command means loop will be start from 0 and will be end .....?
It will end at $#sort_start.
and what steps it will br take
It will step by 1.
.like if i want that this loop works
like
for (my $i=0; $i<=scalar(sort_start);$i+=2){
than how you can write this command in that way?
for (my $i=0; $i<=$#sort_start; $i+=2){
There are a few things you seem not to understand.
Given an array:
my @a = ( 1, 2, 3, 4, 5 );
These things are true:
1. @a == 5
2. $#a == 4
3. $a[0] == 1
4. $a[4] == 5
5. $a[$#a] == 5
6. $a[@a] == $a[5], which is undefined, because
it is accessing an element beyond the last one.
Rita said:Thanks,yes i don't know that $#a means length of array.Brad said:Rita said:John W. Krahn wrote:
Also, in older versions of Perl the list created by 0..$#sort_start would have
to be stored entirely in memory.
But this command means loop will be start from 0 and will be end .....?
It will end at $#sort_start.
and what steps it will br take
It will step by 1.
.like if i want that this loop works
like
for (my $i=0; $i<=scalar(sort_start);$i+=2){
than how you can write this command in that way?
for (my $i=0; $i<=$#sort_start; $i+=2){
There are a few things you seem not to understand.
Given an array:
my @a = ( 1, 2, 3, 4, 5 );
These things are true:
1. @a == 5
2. $#a == 4
3. $a[0] == 1
4. $a[4] == 5
5. $a[$#a] == 5
6. $a[@a] == $a[5], which is undefined, because
it is accessing an element beyond the last one.
$# is the last array INDEX...not the length of the array.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.