I have data in my table of users ("nickname" field):
User Name
username2
username1
UserName
username4
username3
I want to sort it like this:
username1
username2
username3
username4
UserName
User Name
First to have text and numeric order, and then only text.
How can I do this? Thanks for any help!
According to documentation: http://redis.io/commands/SORT
Try:
SORT nickname
Well, there are a few things you must clear up about the solution:
1) The kind of data structure you will use
Before dealing with SORT, you must store the data in some Redis structure. You have plenty of options for this case. Redis native data structures are: hashes, lists, sets, sorted sets and simple key/value.
You could use lists for this, like:
lpush mylist "User Name" username2 username1 UserName username4 username3
Notice that you have to enclose spaces in values with double quotes.
You could also use sets, the main difference between sets and lists is that sets only store distinct values (see the example below, in which a duplicated value is stored once):
127.0.0.1:6379> sadd sample_set avalue avalue
(integer) 1
127.0.0.1:6379> smembers sample_set
1) "avalue"
So with sets, you would go like:
sadd myset "User Name" username2 username1 UserName username4 username3
2) SORT lexicographically
You will use SORT, now when you want to sort lexicographically you must use the ALPHA modifier:
127.0.0.1:6379> sort mylist alpha
1) "User Name"
2) "UserName"
3) "username1"
4) "username2"
5) "username3"
6) "username4"
The sorted order for myset elements is identical.
127.0.0.1:6379> sort myset alpha
1) "User Name"
2) "UserName"
3) "username1"
4) "username2"
5) "username3"
6) "username4"
So you may have noticed that the order you stated you wanted is not quite equal to the common lexicographic order: space, numbers, uppercase letters, lowercase letters which follows the www.asciitable.com order.
Related
I am trying to build a string of values to be inserted into an SQL IN list. For example -
SELECT * FROM TABLE WHERE field IN ('AAA', 'BBB', 'CCC', 'DDD')
The list that I want needs to be constructed from values within a single column of my dataset but I'm struggling to find a way to concatenate those values.
My first thought was to use CASESTOVARS to put each of the values into columns prior to concat. This is simple but the number of cases is variable.
Is there a way to concat all fields without specifying?
Or is there a better way to go about this?
Unfortunately Python is not an option for me in this instance.
A simple sample dataset would be -
CasestoConcat
AAA
BBB
CCC
DDD
You can use the lag function for this.
First creating a bit of sample data to demonstrate on:
data list free/grp (F1) txt (a5).
begin data
1 "aaa" 1 "bb" 1 "cccc" 2 "d" 2 "ee" 2 "fff" 2 "ggggg" 3 "hh" 3 "iii"
end data.
Now the following code makes sure that rows that belong together are consecutive. You can also sort by any other relevant variable to keep the combined text in a specific order.
sort cases by grp.
string merged (A1000).
compute merged=txt.
if $casenum>1 and grp=lag(grp) merged=concat(rtrim(merged), " ", rtrim(lag(merged))).
exe.
At this point if you want to just keep the line that has all the concatenated texts, you can use this:
add files /file=* /by grp /last=lst.
select if lst=1.
exe.
I have a field set that contains bill numbers and I want to sort them first alphabetically then numerically.
For instance I have a column "Bills" that has the following sequence of bills.
- HB200
- SB60
- HB67
Desired outcome is below
- HB67
- HB200
- SB60
How can I use sorting in SSRS Group Properties to have the field sort from [A-Z] & [1 - 1000....]
This should be doable by adding just 2 separate Sort options in the group properties. To test this, I created a simple dataset using your examples.
CREATE TABLE #temp (Bills VARCHAR(20))
INSERT INTO #temp(Bills)
VALUES ('HB200'),('SB60'),('HB67')
SELECT * FROM #temp
Next, I added a matrix with a single row and a single column for my Bills field with a row group.
In the group properties, my sorting options are set up like this:
So to get this working, my theory was that you needed to isolate the numeric characters from the non-numeric characters and use each in their own sort option. To do this, I used the relatively unknown Regex Replace function in SSRS.
This expression gets only the non-numeric characters and is used in the top sorting option:
=System.Text.RegularExpressions.Regex.Replace(Fields!Bills.Value, "[0-9]", "")
While this expression isolates the numeric characters:
=System.Text.RegularExpressions.Regex.Replace(Fields!Bills.Value, "[^0-9]", "")
With these sorting options, my results match what you expect to happen.
In the sort expression for your tablix/table which is displaying the dataset, set the sort to something like:
=IIF(Fields!Bills.Value = "HB67", 1, IIF(Fields!Bills.Value = "HB200", 2, IIF(Fields!Bills.Value = "SB600", 3, 4)))
Then when you sort A-Z, it'll sort by the number given to it in the sort expression.
This is only a solution if you don't have hundreds of values, as this can become quite tedious to create if there's hundreds of possible conditions.
If a hash has more than one occurrences of identical keys pointing to different values, then how does Ruby determine which value is assigned to that key?
In other words,
hash = {keyone: 'value1', keytwo: 'value2', keyone: 'value3'}
results in
warning: duplicated key at line 1 ignored: :keyone
but how do I know which value is assigned to :keyone?
The last one overwrites the previous values. In this case, "value3" becomes the value for :keyone. This works just as the same with merge. When you merge two hashes that have the same keys, the value in the latter hash (not the receiver but the argument) overwrites the other value.
Line numbers on duplicate key warnings can be misleading. As the other answers here confirm, every value of a duplicated key is ignored except for the last value defined for that key.
Using the example in the question across multiple lines:
1 hash1 = {key1: 'value1',
2 key2: 'value2',
3 key1: 'value3'}
4 puts hash1.to_s
keydup.rb:1: warning: duplicated key at line 3 ignored: :key1
{:key1=>"value3", :key2=>"value2"}
The message says "line 3 ignored" but, in fact it was the value of the key defined at line 1 that is ignored, and the value at line 3 is used, because that is the last value passed into that key.
IRB is your friend. Try the following in the command line:
irb
hash = {keyone: 'value1', keytwo: 'value2', keyone: 'value3'}
hash[:keyone]
What did you get? Should be "value3".
Best way to check these things is simply to try it out. It's one of the great things about Ruby.
This is spelled out clearly in section 11.5.5.2 Hash constructor of the ISO Ruby Language Specification:
11.5.5.2 Hash constructor
Semantics
[...]
b) 2) For each association Ai, in the order it appears in the program text, take the following steps:
i) Evaluate the operator-expression of the association-key of Ai. Let Ki be the resulting value.
ii) Evaluate the operator-expression of the association-value. Let Vi be the resulting value.
iii) Store a pair of Ki and Vi in H by invoking the method []= on H with Ki and Vi as the arguments.
another question about hadoop. Is it possible for reducing a list to a map? I mean I have al list like this after the map()
KEY: VALUE:
aaa word
string
word
text
string
word
is it possible to reduce the list to the following structure?
KEY: VALUE:
aaa word, 3
string, 2
text, 1
thanks
manuel
What I would do is the following: due to you are trying to implement the typical word count but on a list that is associated to a key, I would extend such wordwount example by producing at the output of the mappers (key,value) pairs such as:
aaa-word,1
aaa-string,1
aaa-word,1
aaa-text,1
aaa-string,1
aaa-word,1
I.e. I would add the aaa information to all the output pairs. Then, the reducer would behave as usual: by receiving lists of values whose keys are the same; then, the common key is splited into aaa and the word; in addition, the length of the list is returned, which is concatenated to the word.
(aaa-word,1),(aaa-word,1),(aaa-word,1)-->(aaa,word-3)
(aaa-string,1),(aaa-string,1)-->(aaa,string-2)
(aaa-text,1)-->(aaa,text-1)
hello i have i want to do something like this.
i have 4 rows with unique id 1,2,3,4 all four rows contains some string like
option1,option2,option3,option4
now i want to add "a ) " to the option1, "b ) " to the option2 and so on so is there a way i can do this with a query.currently i am adding these to a lots of rows manually
It's not clear exactly by what logic you want to select the letter to prepend to field somestring, but if for example it's a "Caesar's cypher" (1 gives 'a', 2 gives 'b' etc) based on the id field, as your question suggests, then this should work:
UPDATE sometable
SET somestring = (
substr('abcdefghijklmnopqrstuvwxyz', id, 1) ||
' ) ' || somestring)
WHERE id <= 26;
...for no more than 26 rows of course, since beyond that the logic must change and obviously we can't guess just how you want to extend it (use id modulo 26 + 1, use more characters than just lowercase letters, or ...?) since you give no clue on why you want to do this.