Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
You are given a dictionary crypt of type [String:String] which has values for all lowercase letters. The crypt dictionary represents a way to encode a message. For example, if crypt["h"] = "#" and crypt["i"] = "!" the encoded version of the message "hi" will be "#!".
The thing is that i have to Write code that would take any string containing only lower case letters and spaces and encode it using the crypt dictionary. I have successfully failed trying to write the code so i ended up just using a single print statement
//print(crypt["h"]!,crypt["i"]!).
If you have any idea you would like to share, please do so.
Thank you
Does this do what you're looking for:
let message = "hi"
let encryptedMessage = message.map { crypt[String($0)]! }.joined()
If you're unfamiliar with it, mapping a string iterates through each character, doing something to it, and returning that string. $0 refers to the first parameter (in this case #1 of 1, but 0-indexed).
As Dopapp suggests, map is the most elegant solution. If you want to see the steps broken out a bit, you can do it the long way.
var message = "hi"
var crytpedMessage = ""
for char in message {
let newChar = crypt[String(char)]
cryptedMessage.append(newChar)
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I could not transfer my code to the site as I wrote it, but I placed your photo Please can you help me?
You are getting the error because you are trying to pass an uninitialized String to a Text-Widget (Naber). A Text-Widget can not handle uninitialized Strings which have the value null.
Therefore try initializing the String 'Naber' directly like:
String Naber = 'Some text';
This should work.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to fetch a part of string from a string. E.g. I have a string say sampleStringUrl which holds "http://some.something.net/update/user". So, I want to get only "some.something.net" from sampleStringUrl.
Like that you want to get Host URL.
var urlString = "https://stackoverflow.com/questions/6162522/get-the-domain-part-of-an-url-string"
var url = URL(string: urlString)
var domain = url?.host
print(domain!) //stackoverflow.com
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am using Mojolicious Perl framework in my application. I want to store an array in session, but is not successful.
my #returnResult;
$returnResult['fn'] = $decoded->{'fn'};
$returnResult['ln'] = $decoded->{'ln'};
$self->session(returnResult => #returnResult);
Please help.
See hashes in Modern Perl and perldata.
my %return_result;
$returnResult{fn} = $decoded->{fn};
$returnResult{ln} = $decoded->{ln};
or
my %return_result = (
fn => $decoded->{fn},
ln => $decoded->{ln},
);
or simply
# http://perldoc.perl.org/perl5200delta.html#New-slice-syntax
my %return_result = %$decoded{qw(fn ln)};
You do not get automatic references like in other languages. Use the \ operator.
$self->session(returnResult => \%return_result);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
var nsarray:[NSMutableDictionary] = [["object":["uid":["age":"26","gender":"male"]]]]
print(nsarray[0]["object"])
That is how it looks. I want to get the value "uid", so when it prints it is just "uid". Currently it is printing:
"uid":["age":"26","gender":"male"]
I want to get the "uid" value. Meaning when it prints it is just "uid". "uid" is a placeholder for a unique ID so I won't know what the uid is.
It looks like "object" key contains another dictionary, which has exactly one element. To get the first key, call allKeys to get keys, convert them to Array, and pick the the initial element:
let d = nsarray[0]["object"] as! NSDictionary
print(Array(d.allKeys)[0])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want remove common element from array. For example:
array1 =
[
{'id'=>78597,'data'=>'great'}
];
array2=
[
{'id'=>78345,'data'=>'first'},{'id'=>78597,'data'=>'great'},
{'id'=>78355,'data'=>'second'}
]
Now key Id '78597' is common in both array
Now i to want remove that element from array2 based on the key 'id'. The examples I referred where all single dimension.
You can build %seen hash lookup and filter #$array2,
my %seen;
#seen{ map $_->{id}, #$array1 } = ();
#$array2 = grep { !exists $seen{$_->{id}} } #$array2;