I am trying to sort the "fields" in a hash.
For example,
mykey, cde, firstone
mykey, abcde, secondone
mykey, bcde, thirdone
I want to sort the fields(cde, abcde, bcde) in alphabet order, but there is no way to do so.. If anyone knows about this, please help me.
If there is no way to solve this, I am thinking about changing names of key&values.. and use zadd instead of hash. If you have a better solution, please give me an advice here.
Hash field names are not sortable-by easily - there is no native command to do so and the order in which fields are returned (e.g. with HGETALL) is for all intents and purposes random.
While Sorted Sets are preferable when it comes to sorting, you could work around this with use of a Lua script that will perform lexical sorting the Hash's fields. For example:
$ cat hashsort.lua
local r = redis.call('HGETALL',KEYS[1])
local t = {}
for i=1,#r,2 do
t[#t+1] = { field = r[i], value = r[i+1] }
end
table.sort(t, function(a,b) return a.field < b.field end)
r = {}
for _, v in pairs(t) do
r[#r+1] = v.field
r[#r+1] = v.value
end
return r
$ redis-cli HMSET hash z 99 ee 55 e 5 a 1 b 2 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ justsomethinglongsohashmaxzipwillbeexceeded
OK
$ redis-cli --eval hashsort.lua hash
1) "a"
2) "1"
3) "b"
4) "2"
5) "e"
6) "5"
7) "ee"
8) "55"
9) "z"
10) "99"
11) "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
12) "justsomethinglongsohashmaxzipwillbeexceeded"
Related
I'm studying a (mobile) app where I need to get a user PIN:
a numeric "passcode" of 6/8 ciphers, input with something like this UI:
So, in a registration step, the user configure his one passcode (as it would be a password).
Let say the passcode must have a fixed size (say 8 ciphers: ********)
My question is related to a possible algorithm to verify/check the number that user choose, giving a bad rank in case of repeated ciphers or standard cipher patterns (12345678, 00001111), easily predicible by a malicious crackers...
Any idea for such an algorithm ?
At firs glance the algorithm could discourage (bad rank) a passcod containing repeated ciphers, simething like:
00117788
88886611
or "usual" ascending/descending patterns as:
12345678
98765432
Or numeric patterns related to personal, by example in my case, I'm born in 02 September 1963, so it could be a bad idea to have as passcode:
02091963
Instead, sequence that appear to me as "good" could be by example these one:
18745098
90574808
07629301
Collateral question: do you think that a numeric passcode of let say 8 ciphers could be an acceptable solution as "password" to validate a payment transaction ?
BTW, I'm coding in Ruby.
thanks for your patience!
giorgio
For your first 2 cases:
Number of repeated consecutive characters in the string:
str = "00117788"
str.chars.each_cons(2).select {|a,b| a == b}.count
#=> 4
Or as #CarySwoveland pointed out this will have the same result
str.size - str.squeeze.size
#=> 4
Number of incremented characters
str = "12345678"
str.chars.map(&:to_i).each_slice(2).select {|a,b| (a + 1) == b || (a - 1) == b }.count
#=> 4
#note this will also return 4 for "12563478"
# you could also use str.chars.map(&:to_i).each_cons(2).select {|a,b| (a + 1) == b || (a - 1) == b }.count
# This will return 7 for "12345678" and still return 4 for "12563478"
You could combine the above 2 as well like
str = "00117788"
str.chars.map(&:to_i).each_cons(2).select {|a,b| (a + 1) == b || (a - 1) == b || a == b }.count
#=> 6
As for the "personal" issue if you have the birth day then something as simple as this should work:
require 'date'
birth_day = Date.new(1963,9,2)
str = "02091963"
str == birth_day.strftime("%d%m%Y")
#=> true
Although for the last one I would suggest comparing multiple formats e.g. %Y%m%d and %m%d%Y etc. you could even do something like
str.chars.sort == birth_day.strftime("%d%m%Y").chars.sort
#=> true
To make sure they don't just use those numbers in some jumbled format.
Hopefully this would get you started since I don't know what your thresholds are for "good" and "bad" these are just suggestions for checking the values. Although it seems the definition for "good" should just be not "bad". Sort of like a validity check.
If I were to suggest a score of < 4 using methods 1 and 2 (or the combination method) && not an assortment of birth_day numbers would probably be sufficient e.g.
def tester(str,birth_date)
return false if ![6,8].include?(str.size)
b_day = birth_date.strftime("%Y%m%d").chars.sort
str.chars.map(&:to_i).each_cons(2).select do |a,b|
(a + 1) == b ||
(a - 1) == b ||
a == b
end.count < 4 && b_day != str.chars.sort
end
tester("00112233",Date.new(1963,9,2))
#=> false
tester("18745098",Date.new(1963,9,2))
#=> true
Seems like it works with your examples
arry = ["00117788","88886611","12345678","98765432","02091963","18745098","90574808","07629301"]
Hash[arry.map{|v| [v,tester(v,Date.new(1963,9,2))]}]
#=>=> {"00117788"=>false, "88886611"=>false,
"12345678"=>false, "98765432"=>false,
"02091963"=>false, "18745098"=>true,
"90574808"=>true, "07629301"=>true}
I have gone through many questions and Google results but couldn't find the solution.
I am trying to sort a table using table.sort function in Lua but I can't figure out how to use it.
I have a table that has keys as random numeric values. I want to sort them in ascending order. I have gone through the Lua wiki page also but table.sort only works with the table values.
t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" }
I want it like:
t = { [7]="qwe", [23]="fgh", [223]="asd", [543]="hjk" }
You cannot set the order in which the elements are retrieved from the hash (which is what your table is) using pairs. You need to get the keys from that table, sort the keys as its own table, and then use those sorted keys to retrieve the values from your original table:
local t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" }
local tkeys = {}
-- populate the table that holds the keys
for k in pairs(t) do table.insert(tkeys, k) end
-- sort the keys
table.sort(tkeys)
-- use the keys to retrieve the values in the sorted order
for _, k in ipairs(tkeys) do print(k, t[k]) end
This will print
7 qwe
23 fgh
223 asd
543 hjk
Another option would be to provide your own iterator instead of pairs to iterate the table in the order you need, but the sorting of the keys may be simple enough for your needs.
What was said by #lhf is true, your lua table holds its contents in whatever order the implementation finds feasible. However, if you want to print (or iterate over it) in a sorted manner, it is possible (so you can compare it element by element). To achieve this, you can do it in the following way
for key, value in orderedPairs(mytable) do
print(string.format("%s:%s", key, value))
end
Unfortunately, orderedPairs is not provided as a part of lua, you can copy the implementation from here though.
The Lua sort docs provide a good solution
local function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
Then you traverse the sorted structure
local t = { b=1, a=2, z=55, c=0, qa=53, x=8, d=7 }
for key,value in pairsByKeys(t) do
print(" " .. tostring(key) .. "=" .. tostring(value))
end
There is no notion of order in Lua tables: they are just sets of key-value pairs.
The two tables below have exactly the same contents because they contain exactly the same pairs:
t = { [223] = "asd" ,[23] = "fgh",[543]="hjk",[7]="qwe"}
t = {[7]="qwe",[23] = "fgh",[223] = "asd" ,[543]="hjk"}
I have a hash
h = {}
h.compare_by_identity
h[2.51] = 1
h1[2.51] = 2
Edit: h1[2.51] = 2 should be h[2.51] = 2
it is ok with duplicate key. But when i use
Hash[h.sort]
it return only one value with key like
{2.51=>2}
is there any way to get the two values from the hash in sorted order?
Starting with ruby version 2.0 the key 2.51 is actually the same object (because of ruby internal caching) in both assignments. Try to output 2.51.object_id for both cases and it will output the same id.
Since it can't be done with floats, turn them into strings:
h = {}
h.compare_by_identity
h[2.51.to_s] = 2
h[2.51.to_s] = 1
p h.sort # => [["2.51", 1], ["2.51", 2]]
The "how to sort a table in Lua" question isn't new, but the answers I found can't help me out, maybe you can.
I got this Table:
table = {} -- some kind of database
table[1] = {table.containing.table.with.even.more.tables.inside}
table[9] = {table.containing.table.with.even.more.tables.inside}
table[13] = {table.containing.table.with.even.more.tables.inside}
table[15] = {table.containing.table.with.even.more.tables.inside}
table[45] = {table.containing.table.with.even.more.tables.inside}
table[3254] = {table.containing.table.with.even.more.tables.inside}
Now I want to iterate through "table", check for an specified boolean it contains and if so, run a method with parameters from some subtabels.
for key, value in pairs(table) do
print(key)
end
Is something like:
9 13 1 3254 45 15
As far as I know, that's because Lua iterates through hashvalues(right?).
My Idea was:
sorted_table = {} -- shall point to table, with sorted keys
for i = 0, #table do -- from 0 to the last key of table (some write #table is the last key, some write it's the number of contained keys, I don't know. If you do, please tell me.)
if table[i] then -- for runs every number from i to #table, if i is a key, bingo.
table.insert(sorted_table,(table[i])) -- first key found -> sorted_table[1], second -> sorted_table[2]....
end
end
for k,v in pairs(sorted_table) do
print(key)
end
I got no error, it just jumps over the function and nothing happens. When I print #table, it prints 0. #table is in another file but it's not local but used at other location in the functionfile, so,.. this is weird.
EDIT
Mh strange. I threw some debugs, #table is nil, but in a pairs(table) just under the code, everything works fine.
**SOLUTION-EDIT**
local sorted_table = {}
for k, v in pairs(original_table) do
table.insert(sorted_table, k)
end
table.sort(sorted_table)
for k, v in ipairs(sorted_table) do
print(original_table[v])
end
A little try of explanation:
#table does not necessarily return the length of a table. A table element gets a default key if added in a table without a special key. These keys start from 1 and go up to n. If there is a gap between two keys, when you give your own key, #table will return the key right before that gap.
Example:
t = {'one', 'two', 'three'} -- would be a table like 1 - one, 2 - two, 3 - three
print(#t) -- the last key -> 3, here it works
t2 = {'one', 'two', [4] = 'four'} -- would be a table like 1 - one, 2 - two, 4 - four
print(#t2) -- the last key without a gap -> 2, does not work
Same with pairs(table) and ipairs(table). ipairs iterates from key 1 to n without a gap, pairs iterates over all keys. That's why the solution works.
You can set an own metamethod for the table (__len) to use # for the right length.
And remember that your table keys start at 1 by default.
Hope it helped a bit to unterstand the problem here.
SOLUTION
local sorted_table = {}
for k, v in pairs(original_table) do
table.insert(sorted_table, k)
end
table.sort(sorted_table)
for k, v in ipairs(sorted_table) do
print(original_table[v])
end
I need to sort a column in my Access2010 Query correctly. It is a Textcolumn containing Strings with numbers like "CRMPPC1". The text length may vary within the column.
When I sort this it looks like
CRMPPC1
**CRMPPC10**
CRMPPC2
CRMPPC3
CRMPPC4
....
But what I need is
CRMPPC1
CRMPPC2
CRMPPC3
CRMPPC4
....
**CRMPPC10**
Can anybody help me out (preferably with SQL)? I tried various approaches like VAL, CAST etc. but nothing works so far.
If the number of characters in the text prefix is variable then I don't think there is a pure Access SQL solution, but the VBA function
Public Function ExtractNumber(textString As Variant) As Long
Dim s As String, i As Long
s = Nz(textString, "")
For i = 1 To Len(s)
Select Case Mid(s, i, 1)
Case "0" To "9"
Exit For
End Select
Next
If i > Len(s) Then
ExtractNumber = 0
Else
ExtractNumber = Val(Mid(s, i))
End If
End Function
would allow you to use a query like this
SELECT textTest.*
FROM textTest
ORDER BY ExtractNumber([textColumn]);
Try out using this 'hack' for natural order sorting. It only works for two characters, but you can modify it for more of course.
'''' ONLY WORKS WITH LAST TWO CHARACTER NUMBERS AT THE END. IF YOU HAVE THREE NUMBERS IT WILL BREAK'''''''
Function PadNumberForNatSort(strToMod) As String
If IsNumeric(Right(strToMod, 1)) = True Then
If IsNumeric(Mid(strToMod, Len(strToMod), 1)) = True And IsNumeric(Mid(strToMod, Len(strToMod) - 1, 1)) = True Then
PadNumberForNatSort = strToMod
Else
PadNumberForNatSort = Mid(strToMod, 1, Len(strToMod) - 1) & "0" & Mid(strToMod, Len(strToMod), 1)
End If
Else
PadNumberForNatSort = strToMod
End If
End Function
In your SQL: ORDER BY PadNumberForNatSort([column_name])