Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

clearing the data array in awk
Why doesn't the commented out expression work?  I'm trying to avoid
having to use the repetitive version listed below.

# for ( i = 1; i <= 10; i++ ) {
#    cap[i] = 0
#    }

 cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0

A non-programmer using MKS Toolkit's awk...

z.entropic

Report this thread to moderator Post Follow-up to this message
Old Post
entropic
10-09-04 08:55 PM


Re: clearing the data array in awk
On 2004-10-09, entropic wrote:
> Why doesn't the commented out expression work?  I'm trying to avoid
> having to use the repetitive version listed below.
>
> # for ( i = 1; i <= 10; i++ ) {
> #    cap[i] = 0
> #    }

What makes you think this doesn't work? It works for me in old
awk, nawk and gawk.

>  cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
>
> A non-programmer using MKS Toolkit's awk...

--
Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
 ========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Report this thread to moderator Post Follow-up to this message
Old Post
Chris F.A. Johnson
10-09-04 08:55 PM


Re: clearing the data array in awk

entropic wrote:

> Why doesn't the commented out expression work?  I'm trying to avoid
> having to use the repetitive version listed below.
>
> # for ( i = 1; i <= 10; i++ ) {
> #    cap[i] = 0
> #    }
>
>  cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
>
> A non-programmer using MKS Toolkit's awk...
>
> z.entropic

It doesn't work because you commented it out ;-). Otherwise, it'd do
exactly the same as your single-line version.

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
10-09-04 08:55 PM


Re: clearing the data array in awk
In article <fcb9e60c.0410090744.490e3780@posting.google.com>,
subPlanck@excite.com (entropic) wrote:

> Why doesn't the commented out expression work?  I'm trying to avoid
> having to use the repetitive version listed below.
>
> # for ( i = 1; i <= 10; i++ ) {
> #    cap[i] = 0
> #    }
>
>  cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
>
> A non-programmer using MKS Toolkit's awk...
>
> z.entropic

Looks good to me.  Or did you want to delete the array elements?

for( j=1; j <= 10; j++ ) {
delete cap[j]
}

Then tests like

if ( 1 in cap ) print "first element in cap array exists"

would not print since you just deleted the cap[1] element of the array.
And

for( j in cap ) {
print j, cap[j]
}

would not find entries 1 through 10, as you just deleted them.

But if you just want to have cap 1 through 10 be zeros, then the for
loop you showed should work very well.

Bob Harris

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
10-10-04 08:55 AM


Re: clearing the data array in awk
Ed Morton <morton@lsupcaemnt.com> wrote in message news:<x-WdnYOaruOkrfXcRVn-rg@comcast.com
>...
> entropic wrote:
> 
>
> It doesn't work because you commented it out ;-). Otherwise, it'd do
> exactly the same as your single-line version.
>
> 	Ed.

:-)

that would be too easy...

z.entropic

Report this thread to moderator Post Follow-up to this message
Old Post
entropic
10-13-04 08:55 PM


Re: clearing the data array in awk
Bob Harris <harris@zk3.dec.com> wrote in message news:<harris-AC7BBE.17425309102004@cacnews
.cac.cpqcorp.net>...
> In article <fcb9e60c.0410090744.490e3780@posting.google.com>,
>  subPlanck@excite.com (entropic) wrote:
> 
>
> Looks good to me.  Or did you want to delete the array elements?
>
>     for( j=1; j <= 10; j++ ) {
>         delete cap[j]
>     }
>
> Then tests like
>
>     if ( 1 in cap ) print "first element in cap array exists"
>
> would not print since you just deleted the cap[1] element of the array.
> And
>
>     for( j in cap ) {
>         print j, cap[j]
>     }
>
> would not find entries 1 through 10, as you just deleted them.
>
> But if you just want to have cap 1 through 10 be zeros, then the for
> loop you showed should work very well.
>
>                                         Bob Harris

Since the mystery persists, here's my program (some values, like n and
filenames, are passed on from the command line).  The last loop, now
commented out, doesn't work, since no values are printed, but if the
tedious version is active, everything's OK.  If I delete array
elements by delete cur[i], nothing is printed, either.  I'm baffled...

z.entropic

 ========================================
======================
FNR == 1 { end_game() }
END { end_game() }
OFS = " "

( $10 == "C" || $10 == "D" ) && ( $11 == 133 || $11 == 132 ) {

cap[i] = $6
ene[i] = $7
cur[i] = $8
pot[i] = $9
UpDn[i] = $10
meth[i] = $11

if ( $6 > f6 ) {
f6 = $6
}

fn = FILENAME
i++

# print n values per file
if ( i > n ) {
next
}
k = i
}

function end_game() {
for ( i = 1; i <= k && f6 && cap[i] && cur[i] > 0; i++ ) {

printf "%s %3.3g %s %3.g %1.2f %1.3f %1.3e %1.3e %5.1f %5.1f %5.1f\n",
\
fn, i, UpDn[i], meth[i], pot[i], cap[i]*1000, ene[i]*1000, \
cur[i]/(cap[3]==0?1e+100:cap[3]), \
cap[i]/(cap[2]==0?1e+100:cap[2])*100, \
cap[i]/(cap[3]==0?1e+100:cap[3])*100, \
cap[i]/(cap[9]==0?1e+100:cap[9])*100

}
if ( f6 != "" ) {
print "\n"
}
fn = f6 = ""
i = k = 1

#for ( i = 1; i <= 30; i++ ) {
#    cap[i]=0
#    ene[i]=0
#    cur[i]=0
#    pot[i]=0
#    UpDn[i]=0
#    meth[i]=0
#    }

 cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
 cap[11]=cap[12]=cap[13]=cap[14]=cap[15]=
cap[16]=cap[17]=cap[18]=cap[19]=cap[
20]=0
 cap[21]=cap[22]=cap[23]=cap[24]=cap[25]=
cap[26]=cap[27]=cap[28]=cap[29]=cap[
30]=0
 ene[1]=ene[2]=ene[3]=ene[4]=ene[5]=ene[6
]=ene[7]=ene[8]=ene[9]=ene[10]=0
 ene[11]=ene[12]=ene[13]=ene[14]=ene[15]=
ene[16]=ene[17]=ene[18]=ene[19]=ene[
20]=0
 ene[21]=ene[22]=ene[23]=ene[24]=ene[25]=
ene[26]=ene[27]=ene[28]=ene[29]=ene[
30]=0
 cur[1]=cur[2]=cur[3]=cur[4]=cur[5]=cur[6
]=cur[7]=cur[8]=cur[9]=cur[10]=0
 cur[11]=cur[12]=cur[13]=cur[14]=cur[15]=
cur[16]=cur[17]=cur[18]=cur[19]=cur[
20]=0
 cur[21]=cur[22]=cur[23]=cur[24]=cur[25]=
cur[26]=cur[27]=cur[28]=cur[29]=cur[
30]=0
 pot[1]=pot[2]=pot[3]=pot[4]=pot[5]=pot[6
]=pot[7]=pot[8]=pot[9]=pot[10]=0
 pot[11]=pot[12]=pot[13]=pot[14]=pot[15]=
pot[16]=pot[17]=pot[18]=pot[19]=pot[
20]=0
 pot[31]=pot[32]=pot[33]=pot[34]=pot[35]=
pot[36]=pot[37]=pot[38]=pot[39]=pot[
30]=0
 UpDn[1]=UpDn[2]=UpDn[3]=UpDn[4]=UpDn[5]=
UpDn[6]=UpDn[7]=UpDn[8]=UpDn[9]=UpDn
[10]=0
 UpDn[11]=UpDn[12]=UpDn[13]=UpDn[14]=UpDn
[15]=UpDn[16]=UpDn[17]=UpDn[18]=UpDn
[19]=UpDn[20]=0
 UpDn[21]=UpDn[22]=UpDn[23]=UpDn[24]=UpDn
[25]=UpDn[26]=UpDn[27]=UpDn[28]=UpDn
[29]=UpDn[30]=0
 meth[1]=meth[2]=meth[3]=meth[4]=meth[5]=
meth[6]=meth[7]=meth[8]=meth[9]=meth
[10]=0
 meth[11]=meth[12]=meth[13]=meth[14]=meth
[15]=meth[16]=meth[17]=meth[18]=meth
[19]=meth[20]=0
 meth[21]=meth[22]=meth[23]=meth[24]=meth
[25]=meth[26]=meth[27]=meth[28]=meth
[29]=meth[30]=0

}

Report this thread to moderator Post Follow-up to this message
Old Post
entropic
10-13-04 08:55 PM


Re: clearing the data array in awk
In article <fcb9e60c.0410130600.1dbed2b6@posting.google.com>,
subPlanck@excite.com (entropic) wrote:

I think you are mis-managing the value of 'i'.  It is a global because
you did not make it a local.

See my comments intermixed with your code below

> Bob Harris <harris@zk3.dec.com> wrote in message
> news:<harris-AC7BBE.17425309102004@cacnews.cac.cpqcorp.net>... 
>
> Since the mystery persists, here's my program (some values, like n and
> filenames, are passed on from the command line).  The last loop, now
> commented out, doesn't work, since no values are printed, but if the
> tedious version is active, everything's OK.  If I delete array
> elements by delete cur[i], nothing is printed, either.  I'm baffled...
>
> z.entropic
>
>  ========================================
======================
> FNR == 1 { end_game() }
> END { end_game() }
> OFS = " "
>
>  ( $10 == "C" || $10 == "D" ) && ( $11 == 133 || $11 == 132 ) {
>
>    cap[i] = $6
>    ene[i] = $7
>    cur[i] = $8
>    pot[i] = $9
>    UpDn[i] = $10
>    meth[i] = $11

Where is 'i' initialized after FNR==1 ?

As I read this program, after end_game() runs, 'i' is equal to 31

That means you start storing values in cap[31], ene[31], cur[31], etc...

>    if ( $6 > f6 ) {
>       f6 = $6
>    }
>
>    fn = FILENAME
>    i++
>
> # print n values per file
>    if ( i > n ) {
>       next
>    }
>    k = i
> }
>
> function end_game() {
>    for ( i = 1; i <= k && f6 && cap[i] && cur[i] > 0; i++ ) {
>
> printf "%s %3.3g %s %3.g %1.2f %1.3f %1.3e %1.3e %5.1f %5.1f %5.1f\n",
> \
> fn, i, UpDn[i], meth[i], pot[i], cap[i]*1000, ene[i]*1000, \
> cur[i]/(cap[3]==0?1e+100:cap[3]), \
> cap[i]/(cap[2]==0?1e+100:cap[2])*100, \
> cap[i]/(cap[3]==0?1e+100:cap[3])*100, \
> cap[i]/(cap[9]==0?1e+100:cap[9])*100
>
>    }
>    if ( f6 != "" ) {
>       print "\n"
>    }
>    fn = f6 = ""
>    i = k = 1

Yes you set i to 1 here, but then in the 'for' loop you change it, and
leave its value at 31 when you return from end_game()

I suggest you create some local variables or make sure all your array
indexes are unique for their purpose.

A local in awk can be created by having dummy arguments in the function
argument list.  for example

function myfunction(real_arg, _local_vars_, a, b, c, d, e, f )
{
}

Where real_arg is something I really wanted to pass, but _local_vars_
and all that follow are declared here so that they can be used a local
variables.  And there is nothing magical about _local_vars_, I just
named it that to make it easy for me to see that my local variable
declarations follow.  It is just another local variable with a funny
name.  Nothing more.

Anyway, change your 'for' loops to use some other index ('j' is a nice
variable name, and make it a local variable).

And if 'i' is going to be global variable, then I would suggest giving
it a more meaningful name so that it is more obvious it is global in
nature, and not just 'i' the common throw away for loop index variable.

Bob Harris

> #for ( i = 1; i <= 30; i++ ) {
> #    cap[i]=0
> #    ene[i]=0
> #    cur[i]=0
> #    pot[i]=0
> #    UpDn[i]=0
> #    meth[i]=0
> #    }
>
>   cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
>   cap[11]=cap[12]=cap[13]=cap[14]=cap[15]=
cap[16]=cap[17]=cap[18]=cap[19]=c
ap[2
>  0]=0
>   cap[21]=cap[22]=cap[23]=cap[24]=cap[25]=
cap[26]=cap[27]=cap[28]=cap[29]=c
ap[3
>  0]=0
>   ene[1]=ene[2]=ene[3]=ene[4]=ene[5]=ene[6
]=ene[7]=ene[8]=ene[9]=ene[10]=0
>   ene[11]=ene[12]=ene[13]=ene[14]=ene[15]=
ene[16]=ene[17]=ene[18]=ene[19]=e
ne[2
>  0]=0
>   ene[21]=ene[22]=ene[23]=ene[24]=ene[25]=
ene[26]=ene[27]=ene[28]=ene[29]=e
ne[3
>  0]=0
>   cur[1]=cur[2]=cur[3]=cur[4]=cur[5]=cur[6
]=cur[7]=cur[8]=cur[9]=cur[10]=0
>   cur[11]=cur[12]=cur[13]=cur[14]=cur[15]=
cur[16]=cur[17]=cur[18]=cur[19]=c
ur[2
>  0]=0
>   cur[21]=cur[22]=cur[23]=cur[24]=cur[25]=
cur[26]=cur[27]=cur[28]=cur[29]=c
ur[3
>  0]=0
>   pot[1]=pot[2]=pot[3]=pot[4]=pot[5]=pot[6
]=pot[7]=pot[8]=pot[9]=pot[10]=0
>   pot[11]=pot[12]=pot[13]=pot[14]=pot[15]=
pot[16]=pot[17]=pot[18]=pot[19]=p
ot[2
>  0]=0
>   pot[31]=pot[32]=pot[33]=pot[34]=pot[35]=
pot[36]=pot[37]=pot[38]=pot[39]=p
ot[3
>  0]=0
>   UpDn[1]=UpDn[2]=UpDn[3]=UpDn[4]=UpDn[5]=
UpDn[6]=UpDn[7]=UpDn[8]=UpDn[9]=U
pDn[
>  10]=0
>   UpDn[11]=UpDn[12]=UpDn[13]=UpDn[14]=UpDn
[15]=UpDn[16]=UpDn[17]=UpDn[18]=U
pDn[
>  19]=UpDn[20]=0
>   UpDn[21]=UpDn[22]=UpDn[23]=UpDn[24]=UpDn
[25]=UpDn[26]=UpDn[27]=UpDn[28]=U
pDn[
>  29]=UpDn[30]=0
>   meth[1]=meth[2]=meth[3]=meth[4]=meth[5]=
meth[6]=meth[7]=meth[8]=meth[9]=m
eth[
>  10]=0
>   meth[11]=meth[12]=meth[13]=meth[14]=meth
[15]=meth[16]=meth[17]=meth[18]=m
eth[
>  19]=meth[20]=0
>   meth[21]=meth[22]=meth[23]=meth[24]=meth
[25]=meth[26]=meth[27]=meth[28]=m
eth[
>  29]=meth[30]=0
>
> }

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
10-13-04 08:55 PM


Re: clearing the data array in awk
subPlanck@excite.com (entropic) wrote in message news:<fcb9e60c.0410090744.490e3780@posting
.google.com>...
> Why doesn't the commented out expression work?  I'm trying to avoid
> having to use the repetitive version listed below.
>
> # for ( i = 1; i <= 10; i++ ) {
> #    cap[i] = 0
> #    }
>
>  cap[1]=cap[2]=cap[3]=cap[4]=cap[5]=cap[6
]=cap[7]=cap[8]=cap[9]=cap[10]=0
>
> A non-programmer using MKS Toolkit's awk...
>
> z.entropic

To erase the entire array:

delete cap

If that doesn't work with your awk:

split( "", cap )

Report this thread to moderator Post Follow-up to this message
Old Post
William James
10-15-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:52 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.