Jason Guto <
jaso...@gmail.com> wrote:
> Thinking over what you are telling me and trying hard to understand.
> Programming is not easy ++laugh++.
Actually, programming itself is very easy.
Learning programming, that can be hard (esp. if one has no other prior
background to rely upon.
> I guess what I am finding hard to understand. I am able to get the
> content from the file_content file into the $file_content variable;
> however, I cannot access the variable from within the if-statement.
Except your original code was not "access[ing] the variable from within
the if-statement". It was accessing it after the if statement. And if
the body of the if statement never executed, the variable was never
created, which was the cause of your error message.
> The following code works, and retrieves the context of the '.ameta'
> file and saves it to the 'file_content' variable, which I can produce
> with success in the following code.
>
> ---code----
> set dir "."
> set out1 [glob *]
>
> set file_ext "*.ameta"
> set sort_out [lsort -dictionary $out1]
>
> foreach line $sort_out {
> if [file isfile $line] {
> if [string match $file_ext $line] {
> set file_open [open $line r]
> set file_content [read $file_open]
> close $file_open
> #global file_content
> }
> #puts $file_content
> puts "$line test-1 IS A FILE"
> } else {
> }
> }
> puts $file_content
> exit
> ----code---
Your code above works, but only so long as glob * returns at least one
file that is also a match to the pattern "*.ameta". If you move all of
the "*.ameta" files out of the way and try running it, you will get an
error that the "file_content" variable does not exist at the puts
statement at the bottom.
Since you indicate you are a beginner, have you truly grasped the
meaning of what the "if" statement does yet? Because your answers, and
the issue you see, appear to indicate that you've not "got" the meaning
of the "if" statement.
So, if so, lets simplify things:
--- simple code block ---
1: set line "photo.jpg"
2: set file_ext "*.ameta"
3: if {[string match $file_ext $line]} {
4: set found "Yes!"
5: }
6: puts $found
-------------------------
This is a very simplified version of your code. Now, the part to
internalize (and which I suspect you may not yet have done so) is that
an "if" statement makes a choice about whether to run, or *not to run*,
a block of code (that block being the "body" of the "if" (line 4 in the
simple example above)).
So, we start out at line 1. There is no variable named "found" yet
defined. Line 1 defines a variable "line" and gives it the value
"photo.jpg".
Next we run line 2. Again, no variable named "found" exists, and line
2 creates a variable named "file_ext" and gives it a value "*.ameta".
Next, line 3 is run. Line 3 is an "if". So the test part of the "if"
is run (which in this case is the string match command). It compares
the value of the "file_ext" variable (which does exist) and the value
of the "line" variable (which also exists). But because "line"
contains photo.jpg, which does not match to "*.ameta", string match
returns "false" to the "if" statement.
The if statement (line 3) upon receiving a "false" from its test
command, skips over lines 4 and 5. They are never executed [1]. The
next bit of program code to be run then becomes line 6.
Line 6 attempts to retreive the value of a variable named "found". But
there is no variable named "found" in existance yet, because the
command that would have created a variable named "found" (line 4) was
skipped over by the if statement receiving false.
This tiny bit of code illustrates exactly how and why you are receiving
the error message about your variable not existing. The only command
you have in your entire bit of code that would create the variable is
inside the body of an "if". In the case that the "if" always receives
"false" from its test command, then the command that would actually
create the variable is never executed, and the variable is never
created. Therefore you get the error message about variable does not
exist.
[1] Note for the pedantic, yes, line 5 isn't executed. But for the
explanation the statement holds, it is "skipped over" in one sense.