qwebfub3i4u
qwebfub3i4u

Reputation: 319

Set integer equal to counter and increase counter

I have a set of integers that act as ids for other variables in my code. I wish to create a list of them, numbered from 1 to N:

i_counter = 0

i_counter = i_counter + 1
i_A = i_counter
i_counter = i_counter + 1
i_B = i_counter

...

Is there a way to write these in one line? I'd like to be able to rearrange the code line-by-line to change the order of the counters.

Upvotes: 1

Views: 209

Answers (1)

cbk
cbk

Reputation: 4370

In Julia, each statement is also an expression, so you can just chain together statements to get what you want.

i_counter = 0
i_A = i_counter = i_counter + 1
i_B = i_counter = i_counter + 1

or more elegantly, as in Bogumil's comment

i_counter = 0
i_A = i_counter += 1
i_B = i_counter += 1

In other words, there is no need for anything analogous to Python's new "walrus" operator := in Julia, because every statement such as i_counter = i_counter + 1 is already an expression that returns a value.

Upvotes: 1

Related Questions