12-05-2009 07:48 PM
I have a few inputs which is checked one by one at a time. My problem is that, let say that my inputs are:
tcp udp tcpsink tcp udp null
On my first run, the result would be:
$ns connect $tcp5
Only on my second run that I will get the result that I was expecting:
$ns connect $tcp5 $tcpsink3
The reason is that, I'm using a local variable for tcpsink and null when I'm concatenating everything, so it only stores after it has gone through everything; unless my inputs are:
tcpsink tcp tcp null udp udp
then local variable could register the value first thing when it runs as it goes through "tcpsink" and "null"
My question is, how do I overcome this?
A silly way that I could come up with, is by putting everything in another for loop, let it loop twice so that it registers the local variable on the first run, and use it on it's second run. Is there another way to do it? Thanks 🙂
12-05-2009 08:58 PM
At first I thought you might have a race condition as that is a common problem when using local variables; you don't have any control over when a local variable is read vs. written to.
The layout of your code is very confusing as you have wires running up and down, back and forth. It was impossible to see that the case structures are interdependent on each other. I used block diagram cleanup which sorted out your code so it is now possible to see more clearly the order of execution. See the attached VI to see how much easier it is to read.
Unfortunately, I still don't understand what you are trying to do here. It is still a mess of case structures where the true case may or may not be executed depending on a variety of conditions. I suspect your real problem lies in there as some of the false cases don't pass anything through. So I think you might be losing some data between iterations of the while loop.
What are your indicators for String and String2 really doing? They are hidden, so obviously they aren't for the user to see. It seems like you are just using them as a place holder for data. You should be using a shift register on those strings to carry the data to the next iteration. You already know how to do that because you are using shift registers for your blue integer wires.
12-06-2009 09:42 PM
Hi a01lida,
I believe the problem here is more on the program structure and less on the programming itself. Though I also agree that the readability of the code can be further improved.
From studying it, I noted that the local variable is updated only during calls on "tcpsink". While the output string is updated only on calls of "tcp" or "udp". This is the root of your troubles. From the sequence you had given, (tcp udp tcpsink tcp udp null), the local variable is initialized only on the third iteration while the string you mentioned ($ns connect $tcp5) is printed on the first iteration.This is the main reason why $tcpsink3 is not available on the first run.
Programming-wise, it is considered good practice to initialize all the local variables prior to the execution of your main code. I believe this would also provide the solution to your problem.
Best Regards,
Joshua de la Llana
Applications Engineer
NI ASEAN
12-06-2009 10:54 PM - edited 12-06-2009 10:55 PM
I agree with Joshua that there is a problem with initialization. Of course it is not obvious to me what numbers you want if these have never been written in the code, i.e. if they are run for the first time.
Also, all your formatting could be done much more readable using single formatting primitives. I also don't understand the reason for the two stacked autoindexing loops, since you actually start out with only a 1D array.
A single FOR loop is sufficient, eliminating all that termination checking and incrementing. You only need shift registers for the two strings.
Do you really want these tabs in the output? I think a simple concatenate operation seems more reasonable....
Just some ideas....
