Is both are same?

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.
 
I

it_says_BALLS_on_your forehead

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.

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.
 
P

Paul Lalli

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?

Paul Lalli
 
R

Rita

it_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 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.
What the syntax of first for loop i mean how you use in genrol way like

i know syntax of for loop that
for(start value;end value;step){
Thanks
 
R

Rita

Paul 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?
I did then i asking you No they are not doing same thing .second for
loop giving me error when it runs last time in loop and i didnt find
first for loop format in any book so i asked to you bcoz any Group
member gave me that line .
 
A

A. Sinan Unur

Rita wrote: ....

Why didn't you just try it yourself?

I 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.

This carries over to her programming. She does not stop to think a
little about the problem at hand.

Rita, and others like you: You'll get the most bang per time spent if
you actually stop to think occasionally.

Sinan
 
R

Rita

I 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.
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
 
P

Paul Lalli

Rita said:
it's not like that i just didn't find that for loop

Really? Where did you look?

perldoc perlsyn
contains all the syntax of Perl statements. I suggest you read it
thoroughly before attempting any more Perl programming. A for loop is
really one of the most basic aspects of the language.
so i asked to you is anything wrong in it?

Yes, there is definately something wrong with the way you ask
questions. You don't try to answer them for yourself. You don't
provide all the necessary information. I, and many others, have tried
to get you to tell us EVERYTHING needed. In this case, what you
*should* have posted was "I am getting the following error with the
following code. Can you please explain why?", followed by the EXACT
text of the error (which isn't really an error - it's a warning) and
the EXACT code. Instead, you posted a question to which you already
knew the answer - yes, they do something different.
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??????

You need to understand this. You are not posting to Google Groups.
You are using Google Groups' interface to Usenet. The majority of the
"regulars" in this newsgroup are not using Google Groups' interface.
They are using a traditional (and in many, many ways - better)
newsreading client. Usenet has been around for decades longer than
Google Groups, and it in general - and this individual newsgroup in
particular - has therefore developed its own customs and traditions,
which you continually break with your questioning style.

If you want to use a purely Google Groups forum, they have one:
http://groups.google.com/group/perl-programming

Paul Lalli
 
S

Sherm Pendley

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.

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.

So if you want to use the C-style for from above, you need to compare with
'<', not '<='.

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++) {

sherm--
 
J

John W. Krahn

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.

No.

$ perl -le'
use warnings;
use strict;
my @x = 6 .. 10;
for my $i ( 0 .. $#x ) {
print "\$i = $i: $x[$i]";
$i += 4;
}
for ( my $j = 0; $j <= scalar( @x ); $j++ ) {
print "\$j = $j: $x[$j]";
$j += 4;
}
'
$i = 0: 6
$i = 1: 7
$i = 2: 8
$i = 3: 9
$i = 4: 10
$j = 0: 6
Use of uninitialized value in concatenation (.) or string at -e line 6.
$j = 5:


Also, in older versions of Perl the list created by 0..$#sort_start would have
to be stored entirely in memory.


John
 
R

Rita

John said:
Thanks.

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 .....?
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?
 
R

Rita

Sherm 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.
Thanks
 
J

Jürgen Exner

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
 
R

Rita

Jü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
All right ,Thanks
 
J

Joe Smith

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++) {

But the scalar operator doesn't return the array size by
iterating over all the values; it returns a field in the variable
that holds the array size.

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.

-Joe
 
S

Sherm Pendley

Joe Smith said:
But the scalar operator doesn't return the array size by
iterating over all the values;

I neither stated nor implied that it would do so.
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.

I was under the impression that scalar() was a function call, with all of
the argument stack gymnastics that implies. If that's not the case, then
you're right - moving it out of the loop won't make a difference.

sherm--
 
B

Brad Baxter

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.
 
R

Rita

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.
Thanks,yes i don't know that $#a means length of array.
 
I

it_says_BALLS_on_your forehead

Rita said:
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.
Thanks,yes i don't know that $#a means length of array.

$# is the last array INDEX...not the length of the array.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top