Page 1 of 2

how to get a list of used variables ?

Posted: Fri Sep 05, 2014 8:39 am
by wimapon
How can i get a list of used variables in a program.
once i did it.. but now.... do not know
Wim

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 9:35 pm
by RichAlgeni
It's easy. What you do is you print out your program on paper. Then you go through each line, whenever you see a variable, write it down on another piece of paper. Voila! You have a list of variables!

Now, if you need a sorted list, you need to get 26 more pieces of paper. Go through your list, write all variables that start with 'A' on the first paper, 'B' on the second, and so on. Leave room above and below so you can add the variables in the correct order. When done, you will have a sorted list of variables!

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 9:52 pm
by JHPJHP
In the menu of the IDE: tools/Variable Viewer

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 10:00 pm
by IdeasVacuum
It's always a good idea to have EnableExplicit at the top of your code - the compiler will then let you know if any variable present is not declared.

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 10:33 pm
by Tenaja
IdeasVacuum wrote:It's always a good idea to have EnableExplicit at the top of your code - the compiler will then let you know if any variable present is not declared.
Is there a way to get the opposite of that--which vars are declared but not used?

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 10:50 pm
by luis
Tenaja wrote: Is there a way to get the opposite of that--which vars are declared but not used?
No, would be very useful and it has been requested. It's something the compiler should be able to track with little effort.
But if you don't want to wait hoping it will be implemented maybe now thanks to the /PREPROCESS switch it is something that could be written as an IDE tool.

I'm convinced there are tons of unused variables in current PB sources. :)

Re: how to get a list of used variables ?

Posted: Fri Sep 05, 2014 11:45 pm
by skywalk
Yes, the /PREPROCESS switch enables many new opportunities but the line numbers will be tricky when referring back to multiple source files.
I am also guilty of stray variables.

Re: how to get a list of used variables ?

Posted: Thu Sep 11, 2014 10:51 pm
by wimapon
sorry, was a few days away.

My problem in large programs is always, how can i see that a new to choose variable
is not already used, while programming.
It is very dangerous to use a variable for something else....

So it would be very smart to have a tool to get all the used variables in your program.

I use other compilers. they have a way to get a list of all used variables..
so why does pb not have it.....

if it does not exist, i have to make it myself.....

for example: hour, uur, hours, newhour, oldhour, hours2, hours3, hour_yesterday etc.
sometime i do use more than 10 hour names in a large program....



I will try to use: ( maybe it helps)
EnableExplicit
Define a.d
but i am afraid that i have to read too much variables before i can define a new one...




Maybe the best way is to go to the top of your program and than with the editor
find/replace to have a look.....

Wim

Re: how to get a list of used variables ?

Posted: Fri Sep 12, 2014 7:42 am
by wilbert
Did you overlook the answer from JHPJHP ?
http://www.purebasic.fr/english/viewtop ... 05#p452405
The "Variable Viewer" seems like the easiest way to me.

Re: how to get a list of used variables ?

Posted: Fri Sep 12, 2014 11:04 am
by wimapon
Yes i did overlook.. sorry sorry.

But i do remember that i tried it once and decided "this is nog good anough".
but now i do not remember why.

so i will start to use the variable viewer again.

thank you all for your help

Wim

Re: how to get a list of used variables ?

Posted: Sat Sep 13, 2014 8:38 am
by Gadget
Hi,

I tried using the Tools ... Variable Viewer option and had a couple of problems ...

1. Protected variables within a procedure are not listed. According to the PB manual (below) these should be listed.

2. The manual says that it the option can be configured in the preferences. However, I could find this in preferences.

Am I missing the obvious again ?

PB Manual says:
The variable viewer can display variables , Arrays , lists , Constants , Structures and Interfaces defined in your source code, or any currently opened file. You can configure what exactly it should display in the preferences .
Note: The displaying of variables is somewhat limited for now. It can only detect variables explicitly declared with Define , Global , Shared , Protected or Static .

Re: how to get a list of used variables ?

Posted: Sun Sep 14, 2014 7:11 am
by IdeasVacuum
I will try to use: ( maybe it helps)
EnableExplicit
Define a.d
but i am afraid that i have to read too much variables before i can define a new one...
If you try to define a variable name that is already used the compiler will alert you when you run the code. With EnableExplicit(), un-defined variables trigger an alert.

If you want to create a new var but think you may have already used the var name, use Find to see if it exists - that is better than looking at a list of vars, you could still miss it :)

Re: how to get a list of used variables ?

Posted: Sun Sep 14, 2014 11:13 am
by luis
Gadget wrote: 1. Protected variables within a procedure are not listed. According to the PB manual (below) these should be listed.
I never use this so I've just looked into it. You are right.

Looks like a bug:

Code: Select all

Global var

Procedure a()
 Protected a, b, c
EndProcedure
Only "var" is listed.

Change Protected to Global and "a, b, c" are also listed. Also work with Shared. Not with Protected or Static or Define.

Seems so gigantic I find strange nobody noticed before unless is new... or no one is using it maybe ?

Re: how to get a list of used variables ?

Posted: Sun Sep 14, 2014 1:16 pm
by Thade
luis wrote:
Gadget wrote: Seems so gigantic I find strange nobody noticed before unless is new... or no one is using it maybe ?
I use it and for my needs its exactly as it should be.
I can define 100 times vars i or j or k in 100s of Procedures and they all have nothing to do with the main program.
The variable viewer shall show only variables which are relevant.

If there would be an option to show variables used in each procedure ... that would be another story ... lists for each procedure must then be displayed ... or maybe the variable viewer would have to check if the cursor is located inside a Procedure and display only those variables inside that procedure.

@EnableExlicit propagandists
There are some EnableExplicit Fans here ... I cannot share that enthusiasm ... coding that way leaves BASIC in my view. And with that great IDE with best autocomplete it is not needed at all.
I only type a variable once ... all times later I need that var or array or structure or whatever I get it from the autocomplete drop down window which opens when I type in the first letters.
So it always will be correct and not mis-spelled ... and making logic mistakes will not be covered by EnableExplicit anyway.
Who wants to use EnableExplicit and codes that way, its okay ... but don't give other poeple the feeling they are idiots if they don't, each time they read your posts.
This is something like the political correctness or climate change debate ... keep your vision, but don't spoil other peoples life ... 8)

Re: how to get a list of used variables ?

Posted: Sun Sep 14, 2014 2:21 pm
by luis
Thade wrote: I use it and for my needs its exactly as it should be.
Problem is it doesn't do what's documented.
help wrote:It can only detect variables explicitly declared with Define, Global, Shared, Protected or Static
Thade wrote: I can define 100 times vars i or j or k in 100s of Procedures
I agree and that's one of the reasons why I don't find it much useful as it is (if it were working as documented, I mean).
Thade wrote: @EnableExlicit propagandists
I'm a supporter (I find it essential) more than a propagandist (I don't care if you don't use it as long you don't post bogus code as a consequence).
Can I read it anyway ? I'm reading.
Thade wrote: Who wants to use EnableExplicit and codes that way, its okay ... but don't give other poeple the feeling they are idiots if they don't, each time they read your posts.
I wouldn't feel like an idiot even if you were explicitly (duh) posting that using GOTO is for idiots just because you think so.

Anyway, I don't recall people calling other idiots on the ground of EnableExplicit, but maybe I have missed it.
If I didn't probably you are reading too much in some other kind of remark. But why you should feel like an idiot ?

If it works well for you that's all that count as long I'm not forced to do the same.

There is a but: reality suggests many people not using EnableExplicit post code with the kind of errors (mistyped variables) you don't make.
Not only mistyped variables, but sometimes the implicit order in their declaration causes correctly typed variables to not have the intended scope (a programmer error, but nevertheless...).
In the many cases we (or at least I) have witnessed in the forum, EnableExplicit would have helped them in creating less bugged code.
So when I see someone suggesting to use it, or when I suggest to use it, usually the reason is the code one post above contains errors EnableExplicit would have prevented.
In that case I find it a very good suggestion. I hope you are not against it.
The alternative would be to just suggest: "pay more attention next time". Not nearly as useful.
It's like suggesting to use the purifier when you see an error it would have caught.