checking an array...

  • Follow


Hi,
Please  help me...

Problem1:

I am held up in checking an array. I want to check up some invalid
codes while printing the output. We have some 200 hundred codes. In
this only 100 codes are valid left all invalid.

So i have created an array of invalid 100 codes like following.

awk '{

blah
blah
codes=substr($0,1,3)

invalid = "111,222,333,444,555,666,777,.........and so on"
split(invalid,notvalid,",")

if(!(codes in notvalid){
print $0
}
}' input > output

but it is printing all the lines including invalid codes.

I have solved the problem with the following line
if(codes!=111 && codes!=222...........so one){print $0}

but i feel it is lengthy...

Is there any other way?



Problem 2:

I wrote a function header() to print it at every beginning of the new
page starting.

function header(){
print head1
print dd()
}

function dd(){for (i=1; i<=80; i++); printf("=")}


If I call the function in the program like the following.

awk '{

line++
print $0
if(line==60){print header(); line=0}
}

it is printing an empty line after every header like.


                   head 1
================
                                                    ------------> this
is the empty line
line1
line2
line3
...
...
line60


to get rid of the empty line i am doing at the output as


}' input | awk 'NF>0' > output











0
Reply visitnag (50) 2/28/2010 9:23:44 AM

On 2/28/2010 3:23 AM, nag wrote:
> Hi,
> Please  help me...
>
> Problem1:
>
> I am held up in checking an array. I want to check up some invalid
> codes while printing the output. We have some 200 hundred codes. In
> this only 100 codes are valid left all invalid.
>
> So i have created an array of invalid 100 codes like following.
>
> awk '{
>
> blah
> blah
> codes=substr($0,1,3)
>
> invalid = "111,222,333,444,555,666,777,.........and so on"
> split(invalid,notvalid,",")

The above will create an array indexed by the numbers 1 through 100 containing 
elements 111, 222, etc. You want an array indexed by 111, 222, etc. so you can 
use the "in" operator below. That'd be:

invalid = "111,222,333,444,555,666,777,.........and so on"
c=split(invalid,tmpArr,",")
for (i=1;i<=c;i++)
    notvalid[tmpArr[i]]++
delete tmpArr


>
> if(!(codes in notvalid){
> print $0
> }
> }' input>  output
>
> but it is printing all the lines including invalid codes.
>
> I have solved the problem with the following line
> if(codes!=111&&  codes!=222...........so one){print $0}
>
> but i feel it is lengthy...
>
> Is there any other way?
>
>
>
> Problem 2:
>
> I wrote a function header() to print it at every beginning of the new
> page starting.
>
> function header(){
> print head1
> print dd()
> }
>
> function dd(){for (i=1; i<=80; i++); printf("=")}
>
>
> If I call the function in the program like the following.
>
> awk '{
>
> line++
> print $0
> if(line==60){print header(); line=0}

You don't need an extra "line" variable, just use "!(NR%60)" do do something 
every 60 lines.

> }
>
> it is printing an empty line after every header like.

You have a function dd() that prints a bunch of "=" signs. You then call it AND 
print whatever it returns by doing "print dd()". I guess it must be returning 
something that looks like a newline followed by a blank line. Instead of:

function dd(){for (i=1; i<=80; i++); printf("=")}
....
print dd()

do this:

function dd(){for (i=1; i<=80; i++); printf "="; print ""}
....
dd()

or better:

BEGIN{dd=sprintf("%80s","");gsub(/ /,"=",dd)}
....
print dd

Note in the last version above dd is just a string, not a function.

>
>
>                     head 1
> ================
>                                                      ------------>  this
> is the empty line
> line1
> line2
> line3
> ..
> ..
> line60
>
>
> to get rid of the empty line i am doing at the output as
>
>
> }' input | awk 'NF>0'>  output

Not necessary. The scripts you posted above had a non-awkish style and some 
inefficiencies so if you'd like to post what you end up with after fixing the 
above problems we could probably suggest some other improvements.

     Ed.
0
Reply Ed 2/28/2010 10:02:46 AM


On Feb 28, 3:02=A0pm, Ed Morton <mortons...@gmail.com> wrote:
> On 2/28/2010 3:23 AM, nag wrote:
>
>
>
>
>
> > Hi,
> > Please =A0help me...
>
> > Problem1:
>
> > I am held up in checking an array. I want to check up some invalid
> > codes while printing the output. We have some 200 hundred codes. In
> > this only 100 codes are valid left all invalid.
>
> > So i have created an array of invalid 100 codes like following.
>
> > awk '{
>
> > blah
> > blah
> > codes=3Dsubstr($0,1,3)
>
> > invalid =3D "111,222,333,444,555,666,777,.........and so on"
> > split(invalid,notvalid,",")
>
> The above will create an array indexed by the numbers 1 through 100 conta=
ining
> elements 111, 222, etc. You want an array indexed by 111, 222, etc. so yo=
u can
> use the "in" operator below. That'd be:
>
> invalid =3D "111,222,333,444,555,666,777,.........and so on"
> c=3Dsplit(invalid,tmpArr,",")
> for (i=3D1;i<=3Dc;i++)
> =A0 =A0 notvalid[tmpArr[i]]++
> delete tmpArr
>
>
>
>
>
>
>
> > if(!(codes in notvalid){
> > print $0
> > }
> > }' input> =A0output
>
> > but it is printing all the lines including invalid codes.
>
> > I have solved the problem with the following line
> > if(codes!=3D111&& =A0codes!=3D222...........so one){print $0}
>
> > but i feel it is lengthy...
>
> > Is there any other way?
>
> > Problem 2:
>
> > I wrote a function header() to print it at every beginning of the new
> > page starting.
>
> > function header(){
> > print head1
> > print dd()
> > }
>
> > function dd(){for (i=3D1; i<=3D80; i++); printf("=3D")}
>
> > If I call the function in the program like the following.
>
> > awk '{
>
> > line++
> > print $0
> > if(line=3D=3D60){print header(); line=3D0}
>
> You don't need an extra "line" variable, just use "!(NR%60)" do do someth=
ing
> every 60 lines.
>
> > }
>
> > it is printing an empty line after every header like.
>
> You have a function dd() that prints a bunch of "=3D" signs. You then cal=
l it AND
> print whatever it returns by doing "print dd()". I guess it must be retur=
ning
> something that looks like a newline followed by a blank line. Instead of:
>
> function dd(){for (i=3D1; i<=3D80; i++); printf("=3D")}
> ...
> print dd()
>
> do this:
>
> function dd(){for (i=3D1; i<=3D80; i++); printf "=3D"; print ""}
> ...
> dd()
>
> or better:
>
> BEGIN{dd=3Dsprintf("%80s","");gsub(/ /,"=3D",dd)}
> ...
> print dd
>
> Note in the last version above dd is just a string, not a function.
>
>
>
>
>
>
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 head 1
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0------------> =A0this
> > is the empty line
> > line1
> > line2
> > line3
> > ..
> > ..
> > line60
>
> > to get rid of the empty line i am doing at the output as
>
> > }' input | awk 'NF>0'> =A0output
>
> Not necessary. The scripts you posted above had a non-awkish style and so=
me
> inefficiencies so if you'd like to post what you end up with after fixing=
 the
> above problems we could probably suggest some other improvements.
>
> =A0 =A0 =A0Ed.

Thank you sir. Indexed by my required elements gave the answer. Now i
could understand array in array help (which i asked long back in this
forum). Thank you lot.


problem 2:

calling the function simply as dd() instead of print dd() is giving
the correct result.

But the sprintf(....) is also giving the same blank line. but it
helped me in other way.

I have prepared a bill printing prog. In this the name of the person
only printed and the tail is left blank...(which may lead to name
tampering) with the help of sprintf(....) and gsub(...) i replaced the
blanks followed by the name with a special character like "*"

earlier bill print was like this...

                           date: 12/31/2009

Pay to McDonald


now

                           date: 12/31/2009

Pay to McDonald****************************************

Thank you.
0
Reply nag 2/28/2010 11:52:10 AM

2 Replies
116 Views

(page loaded in 0.063 seconds)

Similiar Articles:













7/25/2012 4:21:39 PM


Reply: