How to just get key only on GNU Smalltalk?
I am currently using sortedCollection that stores dictionary of character (key) and number of occurrence for that character (value). When iterating through sortedCollection, how do I access the key value only?
e.g.
[que last notNil] whileTrue: [
stdout << 'current character is ' *key* << ' and occurs << *val* << ' times.' << nl.
]
Where que
is the sortedCollection that sorts dictionary by value.
My goal is following: let's say que
has:
[$a:20, $e:100]
where first letter is the key of dictionary and second number is the value of dictionary. My output should look something like this:
current character is a and occurs 20 times.
current character is e and occurs 100 times.
I am not sure how to get a
, or the key in dictionary since keys are arbitrary.
smalltalk gnu-smalltalk
add a comment |
I am currently using sortedCollection that stores dictionary of character (key) and number of occurrence for that character (value). When iterating through sortedCollection, how do I access the key value only?
e.g.
[que last notNil] whileTrue: [
stdout << 'current character is ' *key* << ' and occurs << *val* << ' times.' << nl.
]
Where que
is the sortedCollection that sorts dictionary by value.
My goal is following: let's say que
has:
[$a:20, $e:100]
where first letter is the key of dictionary and second number is the value of dictionary. My output should look something like this:
current character is a and occurs 20 times.
current character is e and occurs 100 times.
I am not sure how to get a
, or the key in dictionary since keys are arbitrary.
smalltalk gnu-smalltalk
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
I'm sorry,que
is defined as following:que = sortedCollection new .
que := add: nodeLeaf.
wherenodeLeaf := Dictionary new.
and iterate overkeyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.
– selfPointer
Nov 12 '18 at 15:31
1
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
So you're sayingque
is a sorted collection of dictionaries?
– lurker
Nov 21 '18 at 19:06
add a comment |
I am currently using sortedCollection that stores dictionary of character (key) and number of occurrence for that character (value). When iterating through sortedCollection, how do I access the key value only?
e.g.
[que last notNil] whileTrue: [
stdout << 'current character is ' *key* << ' and occurs << *val* << ' times.' << nl.
]
Where que
is the sortedCollection that sorts dictionary by value.
My goal is following: let's say que
has:
[$a:20, $e:100]
where first letter is the key of dictionary and second number is the value of dictionary. My output should look something like this:
current character is a and occurs 20 times.
current character is e and occurs 100 times.
I am not sure how to get a
, or the key in dictionary since keys are arbitrary.
smalltalk gnu-smalltalk
I am currently using sortedCollection that stores dictionary of character (key) and number of occurrence for that character (value). When iterating through sortedCollection, how do I access the key value only?
e.g.
[que last notNil] whileTrue: [
stdout << 'current character is ' *key* << ' and occurs << *val* << ' times.' << nl.
]
Where que
is the sortedCollection that sorts dictionary by value.
My goal is following: let's say que
has:
[$a:20, $e:100]
where first letter is the key of dictionary and second number is the value of dictionary. My output should look something like this:
current character is a and occurs 20 times.
current character is e and occurs 100 times.
I am not sure how to get a
, or the key in dictionary since keys are arbitrary.
smalltalk gnu-smalltalk
smalltalk gnu-smalltalk
asked Nov 12 '18 at 14:07
selfPointerselfPointer
264
264
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
I'm sorry,que
is defined as following:que = sortedCollection new .
que := add: nodeLeaf.
wherenodeLeaf := Dictionary new.
and iterate overkeyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.
– selfPointer
Nov 12 '18 at 15:31
1
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
So you're sayingque
is a sorted collection of dictionaries?
– lurker
Nov 21 '18 at 19:06
add a comment |
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
I'm sorry,que
is defined as following:que = sortedCollection new .
que := add: nodeLeaf.
wherenodeLeaf := Dictionary new.
and iterate overkeyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.
– selfPointer
Nov 12 '18 at 15:31
1
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
So you're sayingque
is a sorted collection of dictionaries?
– lurker
Nov 21 '18 at 19:06
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
I'm sorry,
que
is defined as following: que = sortedCollection new .
que := add: nodeLeaf.
where nodeLeaf := Dictionary new.
and iterate over keyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.– selfPointer
Nov 12 '18 at 15:31
I'm sorry,
que
is defined as following: que = sortedCollection new .
que := add: nodeLeaf.
where nodeLeaf := Dictionary new.
and iterate over keyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.– selfPointer
Nov 12 '18 at 15:31
1
1
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
So you're saying
que
is a sorted collection of dictionaries?– lurker
Nov 21 '18 at 19:06
So you're saying
que
is a sorted collection of dictionaries?– lurker
Nov 21 '18 at 19:06
add a comment |
1 Answer
1
active
oldest
votes
Assuming that que
is a sorted collection and each element is an Association with the character as the key and the count as the value, as in your [$a:20, $e:100]
example, you could do it like this:
que do: [:each | stdout << 'current character is ' << each key
<< ' and occurs ' << each value << ' times.' << nl]
If que is a Dictionary, use que keysAndValuesDo: [:char :count | stdout << "..." char << "..." count << "..." nl]
.
Depending on your overall application, you could also use a Bag, which is an unordered collection of elements that can appear multiple times (as opposed to a Set, which contains each element only once).
characters := 'hello world' asBag.
characters asSet do: [:each |
stdout << 'current character is ' << each
<< ' and occurs ' << (characters occurrencesOf: each)
<< ' times.' << nl]
You could also have a look at the #sortedByCount method and see whether it suits your case. I cannot tell from the reference how exactly its returned collection is structured, so I will not provide you with guessed example code.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263866%2fhow-to-just-get-key-only-on-gnu-smalltalk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming that que
is a sorted collection and each element is an Association with the character as the key and the count as the value, as in your [$a:20, $e:100]
example, you could do it like this:
que do: [:each | stdout << 'current character is ' << each key
<< ' and occurs ' << each value << ' times.' << nl]
If que is a Dictionary, use que keysAndValuesDo: [:char :count | stdout << "..." char << "..." count << "..." nl]
.
Depending on your overall application, you could also use a Bag, which is an unordered collection of elements that can appear multiple times (as opposed to a Set, which contains each element only once).
characters := 'hello world' asBag.
characters asSet do: [:each |
stdout << 'current character is ' << each
<< ' and occurs ' << (characters occurrencesOf: each)
<< ' times.' << nl]
You could also have a look at the #sortedByCount method and see whether it suits your case. I cannot tell from the reference how exactly its returned collection is structured, so I will not provide you with guessed example code.
add a comment |
Assuming that que
is a sorted collection and each element is an Association with the character as the key and the count as the value, as in your [$a:20, $e:100]
example, you could do it like this:
que do: [:each | stdout << 'current character is ' << each key
<< ' and occurs ' << each value << ' times.' << nl]
If que is a Dictionary, use que keysAndValuesDo: [:char :count | stdout << "..." char << "..." count << "..." nl]
.
Depending on your overall application, you could also use a Bag, which is an unordered collection of elements that can appear multiple times (as opposed to a Set, which contains each element only once).
characters := 'hello world' asBag.
characters asSet do: [:each |
stdout << 'current character is ' << each
<< ' and occurs ' << (characters occurrencesOf: each)
<< ' times.' << nl]
You could also have a look at the #sortedByCount method and see whether it suits your case. I cannot tell from the reference how exactly its returned collection is structured, so I will not provide you with guessed example code.
add a comment |
Assuming that que
is a sorted collection and each element is an Association with the character as the key and the count as the value, as in your [$a:20, $e:100]
example, you could do it like this:
que do: [:each | stdout << 'current character is ' << each key
<< ' and occurs ' << each value << ' times.' << nl]
If que is a Dictionary, use que keysAndValuesDo: [:char :count | stdout << "..." char << "..." count << "..." nl]
.
Depending on your overall application, you could also use a Bag, which is an unordered collection of elements that can appear multiple times (as opposed to a Set, which contains each element only once).
characters := 'hello world' asBag.
characters asSet do: [:each |
stdout << 'current character is ' << each
<< ' and occurs ' << (characters occurrencesOf: each)
<< ' times.' << nl]
You could also have a look at the #sortedByCount method and see whether it suits your case. I cannot tell from the reference how exactly its returned collection is structured, so I will not provide you with guessed example code.
Assuming that que
is a sorted collection and each element is an Association with the character as the key and the count as the value, as in your [$a:20, $e:100]
example, you could do it like this:
que do: [:each | stdout << 'current character is ' << each key
<< ' and occurs ' << each value << ' times.' << nl]
If que is a Dictionary, use que keysAndValuesDo: [:char :count | stdout << "..." char << "..." count << "..." nl]
.
Depending on your overall application, you could also use a Bag, which is an unordered collection of elements that can appear multiple times (as opposed to a Set, which contains each element only once).
characters := 'hello world' asBag.
characters asSet do: [:each |
stdout << 'current character is ' << each
<< ' and occurs ' << (characters occurrencesOf: each)
<< ' times.' << nl]
You could also have a look at the #sortedByCount method and see whether it suits your case. I cannot tell from the reference how exactly its returned collection is structured, so I will not provide you with guessed example code.
answered Nov 12 '18 at 17:30
JayKJayK
1,3511919
1,3511919
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263866%2fhow-to-just-get-key-only-on-gnu-smalltalk%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don't show how do you build the collection que, but it seems that to retrieve keys and values, you should use a SortedDictionary or a sorted collection of Associations
– Carlos E. Ferro
Nov 12 '18 at 14:56
I'm sorry,
que
is defined as following:que = sortedCollection new .
que := add: nodeLeaf.
wherenodeLeaf := Dictionary new.
and iterate overkeyAndValuesDo
, which has the character and its number of occurrence I understand that dictionary is not sorted yet, but I think I saw how to sort dictionary by value on different post, so I'll try that first.– selfPointer
Nov 12 '18 at 15:31
1
@selfPointer Can you update the question with the actual code you have, please? For example, there is no mention of keysAndValuesDo: and how you use it in your question.
– JayK
Nov 12 '18 at 17:12
So you're saying
que
is a sorted collection of dictionaries?– lurker
Nov 21 '18 at 19:06