For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2005 > delete array?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author delete array?
Gernot Frisch

2005-02-01, 8:56 am

how to undo:

hit[1, 2]=12;
hit[3, 5]=123;
....

so that hit becomes empty again?
hit = 0 yields an error...

Thank you,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}


Jürgen Kahrs

2005-02-01, 8:56 am

Gernot Frisch wrote:
> how to undo:
>
> hit[1, 2]=12;
> hit[3, 5]=123;
> ...
>
> so that hit becomes empty again?


In GNU Awk:

delete hit

Otherwise, you have to delete element-wise.

Stephane CHAZELAS

2005-02-01, 8:56 am

2005-02-01, 13:13(+01), Jürgen Kahrs:
> Gernot Frisch wrote:
>
> In GNU Awk:
>
> delete hit
>
> Otherwise, you have to delete element-wise.


split("", hit)

should work with every awk.

--
Stéphane
William James

2005-02-01, 8:55 pm


J=FCrgen Kahrs wrote:
> Gernot Frisch wrote:
>
> In GNU Awk:
>
> delete hit
>
> Otherwise, you have to delete element-wise.

Actually, it also works in Kernighan's Awk and in Mawk.

Jürgen Kahrs

2005-02-01, 8:55 pm

William James wrote:

>
> Actually, it also works in Kernighan's Awk and in Mawk.


Yes, but unfortunately it doesn't work in POSIX awk:

http://www.opengroup.org/onlinepubs...99/xcu/awk.html

The delete statement will remove an individual
array element. Thus, the following code will
delete an entire array:

for (index in array)
delete array[index]

Stephane's solution (split("", hit)) was funny.
William James

2005-02-01, 8:55 pm

J=FCrgen Kahrs wrote
> Stephane's solution (split("", hit)) was funny.


but obvious. I've used that myself. So why was
to ability to do "delete hit" even added to Awk?

Stephane CHAZELAS

2005-02-01, 8:55 pm

2005-02-01, 21:48(+01), Jürgen Kahrs:
[...]
> Stephane's solution (split("", hit)) was funny.


That's the one suggested in gawk manual.

See info -f gawk -n Delete

| All the elements of an array may be deleted with a single statement by
| leaving off the subscript in the `delete' statement, as follows:
|
| delete ARRAY
|
| This ability is a `gawk' extension; it is not available in
| compatibility mode (*note Options::).
|
| Using this version of the `delete' statement is about three times more
| efficient than the equivalent loop that deletes each element one at a
| time.
|
| The following statement provides a portable but nonobvious way to clear
| out an array:(1)
|
| split("", array)
|
| The `split' function (*note String Functions::) clears out the target
| array first. This call asks it to split apart the null string. Because
| there is no data to split out, the function simply clears the array and
| then returns.
|
| *Caution:* Deleting an array does not change its type; you cannot
| delete an array and then use the array's name as a scalar (i.e., a
| regular variable). For example, the following does not work:
|
| a[1] = 3; delete a; a = 3


--
Stéphane
Kenny McCormack

2005-02-01, 8:55 pm

In article <36a89jF4uoqu1U1@individual.net>,
=?ISO-8859-1?Q?J=FCrgen_Kahrs?= <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>William James wrote:
>
>
>Yes, but unfortunately it doesn't work in POSIX awk:
>
> http://www.opengroup.org/onlinepubs...99/xcu/awk.html
>
> The delete statement will remove an individual
> array element. Thus, the following code will
> delete an entire array:
>
> for (index in array)
> delete array[index]
>
>Stephane's solution (split("", hit)) was funny.


But entirely valid. I have sometimes used this trick, when using AWKs that
don't allow array deletion the normal way.

Jürgen Kahrs

2005-02-01, 8:55 pm

Stephane CHAZELAS wrote:

>
>
> That's the one suggested in gawk manual.


Brilliant doc. I often look into the POSIX
doc first but obviously the GNU Awk doc reveals
many useful details.
Jürgen Kahrs

2005-02-01, 8:55 pm

William James wrote:

>
>
> but obvious. I've used that myself. So why was
> to ability to do "delete hit" even added to Awk?


In the 1980s the authors of the Unix tools were
still absorbing new ideas to simplify things.
With the success came standards and POSIX.
It looks so easy to add a new feature.
But then you have to stay compatible,
consistent and compliant.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com