- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk help needed!!!
Operating System - HP-UX
1753784
Members
6945
Online
108799
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2003 01:02 PM
тАО01-28-2003 01:02 PM
Hi awk expoerts,
Thank God the Forums are alive again. I've been working on this problem for two days. This should be simple. I am reading a large number of accounts into an array. Each line actually has the account number, description, total debits, and total credits separated by colons. These lines are sorted by account number so that I can search it quickly using a binary search on just the account number. My problem is that the binary search is not working. I've been over it and over it and it looks perfect. I actually copied it from an example in C that I found in the forums. My problem is that it almost never finds a matching account number.
I have simplified the array in the hope that that someone will see what's wrong. You should be able to cut and paste this and just run it as awk -f bsearch.awk. No input file is needed for this simplified example.
function bsearch(array,low,high,target,try)
{
do
{
try = (low + high) / 2;
if (target <= array[try]) high = try - 1
if (target >= array[try]) low = try + 1
}
while (high >= low)
if (low - high > 1)
{
return(try)
}
else
{
return(-1)
}
}
BEGIN {
knt = 0
while (knt < 100)
{
account_array[knt] = sprintf("%05d",10000 + knt);
++knt;
}
i = 0
while (i < 100)
{
printf("%3d %s\n",i,account_array[i]);
++i
}
i = 0
while (i < 100)
{
key = sprintf("%05d",10000 + i)
j = bsearch(account_array,0,99,key);
printf("%3d %d\n",i,j);
++i
}
}
**********************************************
Thanks for any help,
Ryan
Thank God the Forums are alive again. I've been working on this problem for two days. This should be simple. I am reading a large number of accounts into an array. Each line actually has the account number, description, total debits, and total credits separated by colons. These lines are sorted by account number so that I can search it quickly using a binary search on just the account number. My problem is that the binary search is not working. I've been over it and over it and it looks perfect. I actually copied it from an example in C that I found in the forums. My problem is that it almost never finds a matching account number.
I have simplified the array in the hope that that someone will see what's wrong. You should be able to cut and paste this and just run it as awk -f bsearch.awk. No input file is needed for this simplified example.
function bsearch(array,low,high,target,try)
{
do
{
try = (low + high) / 2;
if (target <= array[try]) high = try - 1
if (target >= array[try]) low = try + 1
}
while (high >= low)
if (low - high > 1)
{
return(try)
}
else
{
return(-1)
}
}
BEGIN {
knt = 0
while (knt < 100)
{
account_array[knt] = sprintf("%05d",10000 + knt);
++knt;
}
i = 0
while (i < 100)
{
printf("%3d %s\n",i,account_array[i]);
++i
}
i = 0
while (i < 100)
{
key = sprintf("%05d",10000 + i)
j = bsearch(account_array,0,99,key);
printf("%3d %d\n",i,j);
++i
}
}
**********************************************
Thanks for any help,
Ryan
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2003 01:05 PM
тАО01-28-2003 01:05 PM
Solution
Hi Ryan:
I laughed when I saw this. There's nothing wrong with your logic just your arithmatic (or awk's).
I'll give you a big hint.
Question:
What's (2 + 5) / 2 in awk?
Answer: 3.5 not 3
You need to use the int function.
I laughed when I saw this. There's nothing wrong with your logic just your arithmatic (or awk's).
I'll give you a big hint.
Question:
What's (2 + 5) / 2 in awk?
Answer: 3.5 not 3
You need to use the int function.
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2003 01:08 PM
тАО01-28-2003 01:08 PM
Re: Awk help needed!!!
I'm a little curious why you are using numeric array indices. You could use awk's associative arrays (actually you are using them because that's all there is) and simply let the account number be your index.
If it ain't broke, I can fix that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-28-2003 01:15 PM
тАО01-28-2003 01:15 PM
Re: Awk help needed!!!
Hi Clay,
Arrrgh.... I feel stupid. The int function fixed it.
Thanks,
Ryan
Arrrgh.... I feel stupid. The int function fixed it.
Thanks,
Ryan
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP