Array variable.

A

Andreu

Please consider the following code:

def myfunct

ie = 0
0.step(il, 3) do |i|
dmy = ilines[i+1]
icatn[ie] = dmy[2,5]
idate[ie] = dmy[18,5]
ie += 1
end

me = 0
0.step(ml, 3) do |j|
dmy = mlines[j+1]
mcatn[me] = dmy[2,5]
mdate[me] = dmy[18,5]
me += 1
end

end


When the program exits the first loop, the contents of array idate is OK.
When the second loop is exited, the array variable idate have the same
contents as mdate ...

Any clue welcomed, thanks, Andreu.
 
B

Brian Adkins

Andreu said:
Please consider the following code:

def myfunct

ie = 0
0.step(il, 3) do |i|
dmy = ilines[i+1]
icatn[ie] = dmy[2,5]
idate[ie] = dmy[18,5]
ie += 1
end

me = 0
0.step(ml, 3) do |j|
dmy = mlines[j+1]
mcatn[me] = dmy[2,5]
mdate[me] = dmy[18,5]
me += 1
end

end


When the program exits the first loop, the contents of array idate is OK.
When the second loop is exited, the array variable idate have the same
contents as mdate ...

Any clue welcomed, thanks, Andreu.

You've left out some important info, but aren't you assigning the same
values to both arrays?

idate[ie] = dmy[18,5]
....
mdate[me] = dmy[18,5]
 
A

Andreu

Brian,
Thanks for your answer.
No, if you look again at the code, you will see that in the first loop
'dmy' is read from an array named 'ilines' and in the second one
is read from another array named 'mlines' so should be different.

Andreu.
 
A

Andreu

PROBLEM SOLVED
At the beginning of my program I have written this line:

idate = mdate = Array.new

but it seems Ruby doesn't support this syntax (like C) and assume
that the two arrays are the same one.

Thanks for your time, Andreu.
 
S

Simon Krahnke

* Andreu said:
PROBLEM SOLVED
At the beginning of my program I have written this line:

idate = mdate = Array.new

but it seems Ruby doesn't support this syntax (like C) and assume
that the two arrays are the same one.

Actually Ruby and C do the same thing here: Assign whatever Array.new
yields to mdate and idate.

mfg, simon .... l
 
C

Charles Calvert

PROBLEM SOLVED
At the beginning of my program I have written this line:

idate = mdate = Array.new

but it seems Ruby doesn't support this syntax (like C) and assume
that the two arrays are the same one.

It's not a difference in syntax, but a difference in how variables are
handled. When you write something like this in C:

int a, b;

a = b = 0;

You are creating two variables on the stack and assigning an integer
to them. The actual value 0 is stored in borth variables because they
are intrinsic types that have been allocated on the stack.

When you write in Ruby:

idate = mdate = Array.new

something different happens. Array.new returns a reference to an
instance of array that has been allocated on the heap. The variable
mdate holds the value of the reference, not the instance itself. This
same reference is then assigned to idate. Now both variables have a
reference to the same instace on the heap.

A reference is really just a pointer with nicer semantics. If you're
familiar with a language that has pointers, like C, then think of it
like this:

int * GetInt()
{
int * i = (int *) malloc(sizeof(int));
return i;
}

int * i, * j;

i = j = GetInt();

Now if you modify j:

*j = 5;
printf("%d", *i); // prints "5"
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,089
Latest member
Ketologenic

Latest Threads

Top