Sponsored Content
Full Discussion: Awk Hash Function.
Top Forums Shell Programming and Scripting Awk Hash Function. Post 302215571 by ramen_noodle on Wednesday 16th of July 2008 06:30:23 PM
Old 07-16-2008
Did you want a pseudo hash algorithm and checksum based on line content or a simple
occurrence sum (already provided above)?
Here's the former.
Code:
 
function makesimplehash(data,seedval, n,x,hashval,store,p) {

                 for (n = 1 ; data[n] != 0 ; n++) {
                     x = sprintf("%d",data[n])
                     if (x == 0) {
                                 gencharsarr(store)
                                 for (p=0 ; p < 10 ; p++) {
                                     if (store[p] == data[n]) {x = p + 1; break;}
                                 }
                     }
                     #printf "Derived value %d from %d\n",x,data[n]
                     hashval += int(x + seedval % (n + 1))
                 }
                 return hashval
}         

function zeroarr(data,nv, x) {
                while (x < nv) {data[x] = 0; x++}
}

function gencharsarr(data, n) {
str="abcdefghijk"
                    for (n=0 ; n < 10 ; n++) {data[n]=substr(str,n,1)}
}

function createhashable(szl, store,str,vn) {
store[0]=""
                        gencharsarr(store)
                        for (vn=0 ; vn < szl + 1; vn++) {
                            if (vn == 0) {str = store[int(1 + rand() * 9)]; continue}
                            str = str "," int(1 + rand() * 10)
                        }
                        return str
} 
                           

BEGIN {
arra[0]=""
cnt=0;sz=0
       if (ARGC == 2 && ARGV[1] == "-generate") {
          srand()
          while (cnt < 20) {
                sz = int(1 + rand() * 7)
                print createhashable(sz)
                cnt++
          }
          exit
        }
}

{ 
FS=","
         
         if ( (num = split($0,arra)) == 0) {printf "At record number %d, malformed field content.\n",NR ; next}
         arra[num + 1] = 0;
         #for (a = 1 ; a <= num ; a++) {print a,arra[a]}
         printf "Hash value for %s = %d.\n",$0,makesimplehash(arra,15003)
         zeroarr(arra,num)
}

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

2. UNIX for Dummies Questions & Answers

BASH, HASH and AWK

Hi, I am working on this idea that I want to process some information from a command dump. Using the dump I will search for a string. If it finds the string, it must post a different/associated string to output/logfile. Example: 'Find "cookie jar"' then 'echo "carpool/tomorrow" >... (2 Replies)
Discussion started by: Corpsehy
2 Replies

3. Programming

Hash Function Speed

I have created my own hash table class, but am looking to speed it up. My current hash function is: int HashTable::hashFunc(const string &key) const { int tableSize = theLists.size(); int hashVal = 0; for(int i = 0; i<key.length(); i++) hashVal =... (7 Replies)
Discussion started by: killerqb
7 Replies

4. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

5. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

6. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

7. Shell Programming and Scripting

awk command hash array not printing

I'm new to awk command. The HASH ARRAY is not printing. Merging 2 files togather. vault_input.txt 3P04_Dep_Inxml:2230 REM02_Dep_Inxml:2200 REM03_Dep_Inxml:2400 REM05:2200 REM06:2200 tst6.txt Nov:10:2115:3P04_Dep_Inxml Nov:10:2129:REM02_Dep_Inxml Nov:10:2235:REM03_Dep_Inxml... (5 Replies)
Discussion started by: Sanj123
5 Replies

8. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies

9. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
curs_inwstr(3X) 														   curs_inwstr(3X)

NAME
inwstr, innwstr, winwstr, winnwstr, mvinwstr, mvinnwstr, mvwinwstr, mvwinnwstr - get a string of wchar_t characters from a curses window SYNOPSIS
#include <curses.h> int inwstr(wchar_t *str); int innwstr(wchar_t *str, int n); int winwstr(WINDOW *win, wchar_t *str); int winnwstr(WINDOW *win, wchar_t *str, int n); int mvinwstr(int y, int x, wchar_t *str); int mvinnwstr(int y, int x, wchar_t *str, int n); int mvwinwstr(WINDOW *win, int y, int x, wchar_t *str); int mvwinnwstr(WINDOW *win, int y, int x, wchar_t *str, int n); DESCRIPTION
These routines return a string of wchar_t characters in wstr, extracted starting at the current cursor position in the named window. Attributes are stripped from the characters. The four functions with n as the last argument return a leading substring at most n bytes long (exclusive of the trailing NUL). Transfer stops at the end of the current line, or when n bytes have been stored at the location ref- erenced by wstr. If the size n is not large enough to store a complete character, an error is generated. NOTES
Note that all routines except winnwstr may be macros. RETURN VALUES
All routines return ERR upon failure. Upon successful completion, the *inwstr routines return OK, and the *innwstr routines return the num- ber of characters read into the string. SEE ALSO
curses(3X), curs_instr(3X), curs_in_wchstr(3X) curs_inwstr(3X)
All times are GMT -4. The time now is 07:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy