Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On the curious behaviour of variables!

Status
Not open for further replies.

menski

Technical User
Oct 13, 2002
18
GB

Given that I have the folloiwing chunk of program:

BEGIN {
FS = ":"
print "\n AWKSEARCH - Start:
for (i=2; i < ARGC; i++) {
if (ARGV == &quot;-d&quot;) {disponly = &quot;y&quot;}
else if (ARGV == &quot;-f&quot;) {fileonly = &quot;y&quot;}
else if (ARGV == &quot;-r&quot;) {recurse = &quot;y&quot;}
else if (ARGV == &quot;-s&quot;) {recurse = &quot;n&quot;}

else if (ARGV == &quot;-x&quot;) {
exc == &quot;&quot; ? exc = ARGV[i+1] :
exc = exc &quot;:&quot; ARGV[i+1]
++i
}
else if (ARGV == &quot;-i&quot;) {
inc == &quot;&quot; ? inc = ARGV[i+1] :
inc = inc &quot;:&quot; ARGV[i+1]
++i
}
else if (ARGV == &quot;-t&quot;) {
txt == &quot;&quot; ? txt = ARGV[i+1] :
txt = txt &quot;:&quot; ARGV[i+1]
++i
}
}

for (i = 2; i < ARGC; i++) delete ARGV
if (disponly == &quot;y&quot;) exit
if (txt == &quot;&quot; && fileonly != &quot;y&quot;) exit

if (exc != &quot;&quot;) {
$0 = exc
print &quot; $0exc: &quot;$0
for (x = 1; x <= NF; x++) {
exclude[x] = $x
print &quot; exclude&quot;x&quot;: &quot;exclude[x]
}
exc = &quot;&quot;
print &quot;x = &quot;x

if (inc != &quot;&quot;) {
$0 = inc
print &quot; $0inc: &quot;$0
for (d = 1; d <= NF; d++) {
include[d] = $d
print &quot; include&quot;d&quot;: &quot;include[d]
}
inc = &quot;&quot;
print &quot;d = &quot;d

if (txt != &quot;&quot;) {
$0 = txt
print &quot; $0txt: &quot;$0
for (t = 1; t <= NF; t++) {
text[t] = $t
print &quot; text&quot;t&quot;: &quot;text[t]
}
txt = &quot;&quot;
print &quot;t = &quot;t

exit

Why is the output of:

> awk -f awksearch dummy -x awk1X1 -t duncan2T1 -x ferret2X3 -i mess4I1 -i pottage5I2 -x soup6x3 -t duck7T2
As follows:

$0exc: awk1X1:ferret2X3:soup6x3
exclude1: awk1X1
exclude2: ferret2X3
exclude3: soup6x3
x = 4
$0inc: mess4I1:pottage5I2
include1: awk1X1
include2: ferret2X3
d = 3
$0txt: duncan2T1:duck7T2
text1: duncan2T1
text2: duck 7T2
t = 3

Am I missing something terribly obvious?

dmk
 
Uncheck the Process TGML box and resubmit your posting. The was taken to to mean turn on italics. CaKiwi
 

Sorry abot that: this should look OK.

BEGIN {
FS = &quot;:&quot;
print &quot;\n AWKSEARCH - Start:

for (i=2; i < ARGC; i++) {
if (ARGV == &quot;-d&quot;) {disponly = &quot;y&quot;}
else if (ARGV == &quot;-f&quot;) {fileonly = &quot;y&quot;}
else if (ARGV == &quot;-r&quot;) {recurse = &quot;y&quot;}
else if (ARGV == &quot;-s&quot;) {recurse = &quot;n&quot;}

else if (ARGV == &quot;-x&quot;) {
exc == &quot;&quot; ? exc = ARGV[i+1] :
exc = exc &quot;:&quot; ARGV[i+1]
++i
}
else if (ARGV == &quot;-i&quot;) {
inc == &quot;&quot; ? inc = ARGV[i+1] :
inc = inc &quot;:&quot; ARGV[i+1]
++i
}
else if (ARGV == &quot;-t&quot;) {
txt == &quot;&quot; ? txt = ARGV[i+1] :
txt = txt &quot;:&quot; ARGV[i+1]
++i
}
}

for (i = 2; i < ARGC; i++) delete ARGV
if (disponly == &quot;y&quot;) exit
if (txt == &quot;&quot; && fileonly != &quot;y&quot;) exit

if (exc != &quot;&quot;) {
$0 = exc
print &quot; $0exc: &quot;$0
for (x = 1; x <= NF; x++) {
exclude[x] = $x
print &quot; exclude&quot;x&quot;: &quot;exclude[x]
}
exc = &quot;&quot;
print &quot;x = &quot;x
}
if (inc != &quot;&quot;) {
$0 = inc
print &quot; $0inc: &quot;$0
for (d = 1; d <= NF; d++) {
include[d] = $d
print &quot; include&quot;d&quot;: &quot;include[d]
}
inc = &quot;&quot;
print &quot;d = &quot;d
}
if (txt != &quot;&quot;) {
$0 = txt
print &quot; $0txt: &quot;$0
for (t = 1; t <= NF; t++) {
text[t] = $t
print &quot; text&quot;t&quot;: &quot;text[t]
}
txt = &quot;&quot;
print &quot;t = &quot;t
}
exit

Why is the output of:

> awk -f awksearch dummy -x awk1X1 -t duncan2T1 -x ferret2X3 -i mess4I1 -i pottage5I2 -x soup6x3 -t duck7T2
As follows:

$0exc: awk1X1:ferret2X3:soup6x3
exclude1: awk1X1
exclude2: ferret2X3
exclude3: soup6x3
x = 4
$0inc: mess4I1:pottage5I2
include1: awk1X1
include2: ferret2X3
d = 3
$0txt: duncan2T1:duck7T2
text1: duncan2T1
text2: duck 7T2
t = 3

Am I missing something terribly obvious?

dmk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top