Re: How to sort array
- From: Dave B <daveb@xxxxxxxxxxxx>
- Date: Sun, 14 Dec 2008 18:20:33 +0100
(I'm replying to you since many of your comments are about the code I posted
originally)
Ed Morton wrote:
When invoking an external command, since you need to close exactly the
same string you're opening, use a variable instead of specifying the
same string twice to make sure you do close it:
cmd = "sort >> _abc.txt"
print a[ele] | cmd
}
close("sort >> _abc.txt");
close(cmd)
Fair enough, I just didn't think it was necessary since the command had to
be used only twice in the program (three times with the close(), ok, but
again that did not seem necessary to me for such a short program which does
only that and certainly does not run the risk of running out of available
file descriptors, at least as it's currently written).
Anyway, it's true that getting into the good habit costs nothing, so it's
better to do things correctly anyway.
while(getline a[++n] < "_abc.txt");
The above doesn't do quite what you think it does in all cases. To be
safe, you'd have to change it to:
while ( (getline a[++n] < "_abc.txt") > 0)
Checking the return value of getline is better, thanks, although in this
case I implicitly assumed it could not produce an error, but again, your
code is admittedly more robust and just few keystrokes longer.
and make sure you read and understand http://tinyurl.com/yn9ka9 if
you're considering using getline.
Agreed, although I don't see how else you could read the contents of the
file into the array in this case without using getline.
Thanks for the remarks.
--
awk 'BEGIN{O="~"~"~";o="=="=="==";o+=+o;x=O""O;while(X++<=x+o+o)c=c"%c";
printf c,(x-O)*(x-O),x*(x-o)-o,x*(x-O)+x-O-o,+x*(x-O)-x+o,X*(o*o+O)+x-O,
X*(X-x)-o*o,(x+X)*o*o+o,x*(X-x)-O-O,x-O+(O+o+X+x)*(o+O),X*X-X*(x-O)-x+O,
O+X*(o*(o+O)+O),+x+O+X*o,x*(x-o),(o+X+x)*o*o-(x-O-O),O+(X-x)*(X+O),x-O}'
.
- Follow-Ups:
- Re: How to sort array
- From: Ed Morton
- Re: How to sort array
- References:
- How to sort array
- From: moonhkt
- Re: How to sort array
- From: Grant
- Re: How to sort array
- From: Dave B
- Re: How to sort array
- From: moonhkt
- Re: How to sort array
- From: Ed Morton
- How to sort array
- Prev by Date: Re: How to sort array
- Next by Date: Re: How to sort array
- Previous by thread: Re: How to sort array
- Next by thread: Re: How to sort array
- Index(es):
Relevant Pages
|