Skip to content

Commit 490ff46

Browse files
authored
Merge pull request #17 from aks/fix-hash-print
fix sort keys when printing a hash
2 parents 30ecee6 + 8052328 commit 490ff46

File tree

8 files changed

+62
-58
lines changed

8 files changed

+62
-58
lines changed

DONE

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
# DONE
2+
# Copyright 2006-2022 Alan K. Stebbens <[email protected]>
3+
#
4+
# A change log for the bash-lib
5+
6+
2022/07/17
7+
* Makefile:
8+
- make invocation path explicitly relative
9+
* hash-utils.sh:
10+
- sort the keys for printing on hash_print
11+
112
2015/05/31
2-
* sh-utils.sh:
13+
* sh-utils.sh:
314
- refactored into talk-utils, arg-utils, run-utils
415
* Makefile:
516
- added the newly refactored modules
@@ -21,7 +32,7 @@
2132
* test-text-utils.sh:
2233
- refactored all the tests to support new STDIN feature
2334
* sh-utils.sh:
24-
- added args_or_stdin function to pass along ars, or
35+
- added args_or_stdin function to pass along ars, or
2536
read from STDIN
2637

2738
2014/06/11

hash-utils.sh

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# hash-utils.sh
2-
# Copyright 2014-2015 Alan K. Stebbens <[email protected]>
2+
# Copyright 2014-2022 Alan K. Stebbens <[email protected]>
33
#
44
# bash script utilities for managing hashes (associative arrays)
55

@@ -313,23 +313,16 @@ print_hash() { "$@" ; }
313313
# hash_print_items VAR
314314
hash_print_items() {
315315
help_args_func hash_help $# || return
316-
local var="$1" k
316+
local var="$1" key val
317317
local -a keys
318318
hash_get_keys $var
319-
for k in "${keys[@]}" ; do
320-
hash_print_item $var "$k"
321-
echo ''
319+
__list_sort keys
320+
for key in "${keys[@]}" ; do
321+
val=`hash_get $var "$key"`
322+
printf "%s['%s']='%s'\n" $var "$key" "$val"
322323
done
323324
}
324325

325-
# hash_print_item VAR KEY
326-
327-
hash_print_item() {
328-
local var="$1" key="$2"
329-
local val=`hash_get $var "$key"`
330-
printf "%s['%s']='%s'" $var "$key" "$val"
331-
}
332-
333326
# _set_args "OPT1 OPT2 ..." "$@"
334327
#
335328
# Scan the arguments looking for OPTn=VAL, and set each OPTn to the

test-hash-utils.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
2-
# Copyright 2006-2014, Alan K. Stebbens <[email protected]>
3-
#
2+
# Copyright 2006-2022, Alan K. Stebbens <[email protected]>
3+
#
44
# Test module for hash-utils.sh
55
#
66

@@ -136,7 +136,7 @@ test_10_print_hash() {
136136
apple banana cherry dog elephant fox giraffe hawk indigo manzana milk november
137137
october december january february march april may june july august
138138
)
139-
hash_init hash10
139+
hash_init hash10
140140
for ((i=0; i<${#words[@]}; i++)) ; do
141141
hash_set hash10 ${words[i]} $i
142142
done

test/phout1.out.ref

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
hash10['april']='17' hash10['apple']='0' hash10['november']='11'
2-
hash10['cherry']='2' hash10['march']='16' hash10['august']='21'
3-
hash10['october']='12' hash10['indigo']='8' hash10['may']='18'
4-
hash10['giraffe']='6' hash10['june']='19' hash10['banana']='1'
5-
hash10['february']='15' hash10['milk']='10' hash10['manzana']='9'
6-
hash10['july']='20' hash10['january']='14' hash10['fox']='5'
7-
hash10['dog']='3' hash10['hawk']='7'
8-
hash10['elephant']='4' hash10['december']='13'
1+
hash10['apple']='0' hash10['february']='15' hash10['manzana']='9'
2+
hash10['april']='17' hash10['fox']='5' hash10['march']='16'
3+
hash10['august']='21' hash10['giraffe']='6' hash10['may']='18'
4+
hash10['banana']='1' hash10['hawk']='7' hash10['milk']='10'
5+
hash10['cherry']='2' hash10['indigo']='8' hash10['november']='11'
6+
hash10['december']='13' hash10['january']='14' hash10['october']='12'
7+
hash10['dog']='3' hash10['july']='20'
8+
hash10['elephant']='4' hash10['june']='19'

test/phout2.out.ref

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
hash10['april']='17' hash10['apple']='0' hash10['november']='11'
2-
hash10['cherry']='2' hash10['march']='16' hash10['august']='21'
3-
hash10['october']='12' hash10['indigo']='8' hash10['may']='18'
4-
hash10['giraffe']='6' hash10['june']='19' hash10['banana']='1'
5-
hash10['february']='15' hash10['milk']='10' hash10['manzana']='9'
6-
hash10['july']='20' hash10['january']='14' hash10['fox']='5'
7-
hash10['dog']='3' hash10['hawk']='7'
8-
hash10['elephant']='4' hash10['december']='13'
1+
hash10['apple']='0' hash10['february']='15' hash10['manzana']='9'
2+
hash10['april']='17' hash10['fox']='5' hash10['march']='16'
3+
hash10['august']='21' hash10['giraffe']='6' hash10['may']='18'
4+
hash10['banana']='1' hash10['hawk']='7' hash10['milk']='10'
5+
hash10['cherry']='2' hash10['indigo']='8' hash10['november']='11'
6+
hash10['december']='13' hash10['january']='14' hash10['october']='12'
7+
hash10['dog']='3' hash10['july']='20'
8+
hash10['elephant']='4' hash10['june']='19'

test/phout3.out.ref

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
hash10['april']='17' hash10['dog']='3' hash10['milk']='10' hash10['may']='18'
2-
hash10['cherry']='2' hash10['elephant']='4' hash10['january']='14' hash10['banana']='1'
3-
hash10['october']='12' hash10['apple']='0' hash10['hawk']='7' hash10['manzana']='9'
4-
hash10['giraffe']='6' hash10['march']='16' hash10['december']='13' hash10['fox']='5'
5-
hash10['february']='15' hash10['indigo']='8' hash10['november']='11'
6-
hash10['july']='20' hash10['june']='19' hash10['august']='21'
1+
hash10['apple']='0' hash10['dog']='3' hash10['indigo']='8' hash10['may']='18'
2+
hash10['april']='17' hash10['elephant']='4' hash10['january']='14' hash10['milk']='10'
3+
hash10['august']='21' hash10['february']='15' hash10['july']='20' hash10['november']='11'
4+
hash10['banana']='1' hash10['fox']='5' hash10['june']='19' hash10['october']='12'
5+
hash10['cherry']='2' hash10['giraffe']='6' hash10['manzana']='9'
6+
hash10['december']='13' hash10['hawk']='7' hash10['march']='16'

test/phout4.out.ref

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
hash10['april']='17' hash10['apple']='0' hash10['november']='11'
2-
hash10['cherry']='2' hash10['march']='16' hash10['august']='21'
3-
hash10['october']='12' hash10['indigo']='8' hash10['may']='18'
4-
hash10['giraffe']='6' hash10['june']='19' hash10['banana']='1'
5-
hash10['february']='15' hash10['milk']='10' hash10['manzana']='9'
6-
hash10['july']='20' hash10['january']='14' hash10['fox']='5'
7-
hash10['dog']='3' hash10['hawk']='7'
8-
hash10['elephant']='4' hash10['december']='13'
1+
hash10['apple']='0' hash10['february']='15' hash10['manzana']='9'
2+
hash10['april']='17' hash10['fox']='5' hash10['march']='16'
3+
hash10['august']='21' hash10['giraffe']='6' hash10['may']='18'
4+
hash10['banana']='1' hash10['hawk']='7' hash10['milk']='10'
5+
hash10['cherry']='2' hash10['indigo']='8' hash10['november']='11'
6+
hash10['december']='13' hash10['january']='14' hash10['october']='12'
7+
hash10['dog']='3' hash10['july']='20'
8+
hash10['elephant']='4' hash10['june']='19'

test/phout5.out.ref

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
hash10['april']='17' hash10['june']='19'
2-
hash10['cherry']='2' hash10['milk']='10'
3-
hash10['october']='12' hash10['january']='14'
4-
hash10['giraffe']='6' hash10['hawk']='7'
5-
hash10['february']='15' hash10['december']='13'
6-
hash10['july']='20' hash10['november']='11'
7-
hash10['dog']='3' hash10['august']='21'
1+
hash10['apple']='0' hash10['hawk']='7'
2+
hash10['april']='17' hash10['indigo']='8'
3+
hash10['august']='21' hash10['january']='14'
4+
hash10['banana']='1' hash10['july']='20'
5+
hash10['cherry']='2' hash10['june']='19'
6+
hash10['december']='13' hash10['manzana']='9'
7+
hash10['dog']='3' hash10['march']='16'
88
hash10['elephant']='4' hash10['may']='18'
9-
hash10['apple']='0' hash10['banana']='1'
10-
hash10['march']='16' hash10['manzana']='9'
11-
hash10['indigo']='8' hash10['fox']='5'
9+
hash10['february']='15' hash10['milk']='10'
10+
hash10['fox']='5' hash10['november']='11'
11+
hash10['giraffe']='6' hash10['october']='12'

0 commit comments

Comments
 (0)