what does double square brackets mean in swift? - arrays

the below is the sample code in swift.
var loadedMessages = [[Message]]()
Message is a custom class. i'm not sure what [[Message]] () is doing.

It is specifying that your variable loadedMessages is an array of arrays that contains Message objects. A JSON representation of loadedMessages might look like:
loadedMessages: [
[ <Message>, <Message>, <Message> ],
[ <Message>, <Message>, <Message> ]
]
A quick Playground implementation of something similar can give you a pretty good introspection of the situation:
var foo = [[String]]()
foo.append(["bar"])
foo[0][0] // reveals "bar"

It means it’s an array of arrays of messages. Think of it in terms of whatever appears between the square brackets being an array of that, and this can include another array.
Alternatively, if you were to write out without the “shorthand” array syntax, it would be Array<Array<Messages>>().

Related

How to conform an Array to 'Identifiable'

I'm confused about how Identifiable arrays work. For instance, I have the following array returned as a response to a function:
Optional (["Jane", "John", "Eric", "Erika", "Motto"])
Now, I want to show this as a list but we know that list items need to conform to Identifiable. How can I conform an array like this to 'Identifiable'? I was looking at this question but it didn't seem to help a lot.
I figured it. simple doing the following should do the job:
#State private var options: [String] = []
// then use this
ForEach(options, id:\.self){ option in
Text(option)

is it possible to reference an index position in a nested array? Ruby

im to Ruby; I see theres been various questions on this topic..but cant find one like this bu I gather multidimensional arrays don't exist in ruby as such. So im working with a nested array for a fictitious data set. is it possible to reference an index position for a child element in a nested array? I've figured iterating through and creating a block for each array is the best way forward (and confirmed this on SO). i see in another article its recommended to use the Narray library...does this work with strings? my first aim is to print out each name and the last area of interest. ive put an example of what im roughly trying to achieve in the example.. many thanks
staff_array = [
["paulBarry",
["programming", "networking", "security", "open source" ,"frameworks"]],
["chrisMuedec",
["testing", "safety systems", "formal systems", "programming languages"]],
["nigelwhite",
["graphics", "imaging", "programming","sign languages","trees"]],
["austinKinsella",
["networks", "wans", "programming", "macintosh", "digital photography"]],
["gerryMoloney",
["placement", "employment", "emerging systems", "webdevelopment"]
]
]
staff.each do |name_array|
# Iterate through the parent array, returning each element sequentially
name_array.each do |interest_element|
# Iterate through each element of the child array returned by the above parent iteration
Example:
puts {#name_array}+ name_array.each do |interest_element|[-1]
end
end
Expected output would be:
Paulbarry: Frameworks
ChrisMuedec: Programming languages
Nigelwhite: Trees
AustinKinsella: Digital photography
GerryMoloney: Webdevelopment.
Ruby has nested arrays. You reference index positions with the [] method starting at 0, no matter the nesting level.
array = [
[
['0.0.0', '0.0.1', '0.0.2'],
['0.1.0', '0.1.1', '0.1.2'],
['0.2.0', '0.2.1', '0.2.2'],
],
[
['1.0.0', '1.0.1', '1.0.2'],
['1.1.0', '1.1.1', '1.1.2'],
['1.2.0', '1.2.1', '1.2.2'],
],
[
['2.0.0', '2.0.1', '2.0.2'],
['2.1.0', '2.1.1', '2.1.2'],
['2.2.0', '2.2.1', '2.2.2'],
],
]
array[0][0][0] # => '0.0.0'
array[0][1][2] # => '0.1.2'
array[2][2][2] # => '2.2.2'
There are also some specific methods like first and last.
array.first.first.first # => '0.0.0'
array.last.last.last # => '2.2.2'
my first aim is to print out each name and the last area of interest.
staff_array.each do |name, interests|
puts "#{name.sub(/./) { |m| m.upcase }}: #{interests.last.capitalize}"
end
Output:
PaulBarry: Frameworks
ChrisMuedec: Programming languages
Nigelwhite: Trees
AustinKinsella: Digital photography
GerryMoloney: Webdevelopment
Perhaps from this first step you can infer how to generalize to answering your other questions.

AMP split array in src bind

I am trying to build a binding in an amp-list. What I need to do is extract the first entry in the array. For instance my json array looks like this:
"Ids": [
"123",
"456"
]
The amp-binding is looking like this:
[src]="'http://any.com?id=' + {{Ids}}"
I want this to end up like:
src="http://any.com?id=123
But is looks like this without any manipulation
src="http://any.com?id=123,456
Looking at the white listed functions, I wondering if I should use Array to String for this.

Include a regex string in an array

I have an array made up of several strings that I am searching for in another array, like so:
strings_array = ["string1", "string2", "string3"]
main_array = [ ## this is populated with string values outside of my script ## ]
main_array.each { |item|
if strings_array.any? { |x| main_array.include?(x) }
main_array.delete(item)
end
}
This is a simplified version of what my script is actually doing, but that's the gist. It works as is, but I'm wondering how I can make it so that the strings_array can include strings made out of regex. So let's say I have a string in the main_array called "string-4385", and I want to delete any string that is composed of string- + a series of integers (without manually adding in the numerical suffix). I tried this:
strings_array = ["string1", "string2", "string3", /string-\d+/.to_s]
This doesn't work, but that's the logic I'm aiming for. Basically, is there a way to include a string with regex like this within an array? Or is there perhaps a better way than this .any? and include? combo that does the job (without needing to type out the complete string value)?
Thank you for any help!
You can use methods like keep_if and delete_if, so if you want to delete strings that match a regex you could do something like this:
array = ['string-123', 'test']
array.delete_if{|n| n[/string-\d+/] }
That will delete the strings in the array that do not match your regex. Same thing with keep_if method.
Hope it helps!
A good way to do this is with Regexp.union, which combines multiple regular expressions into a single regex handy for matching.
patterns = [/pattern1/, /pattern2/, /string-\d+/]
regex = Regexp.union(patterns)
main_array.delete_if{|string| string.match(regex)}

Dynamic array creation in perl [duplicate]

This question already has answers here:
How can I use a variable as a variable name in Perl?
(3 answers)
Closed 7 years ago.
I have read this post and saw that there are various ways to create a dynamic naming of arrays in perl.
http://en.allexperts.com/q/Perl-CGI-1045/dynamic-naming-array-1.htm
as per the link, the code will create dynamic arrays like these:
#yearsSoFar2004,
#yearsSoFar2005,
#yearsSoFar2006 etc.
My requirement is as follows. I want to create a dynamic array like this:
my #a0=();
my #a1=();
my #a2=();
my #a3=();
I currently have it static in my code but i want to make it dynamic. Here is the static code below. Please help me guys. I am a newbie to PERL
my #a=();
my #b=();
my #c=();
my #d=();
Three problems with your request:
my declares a lexical variable at compile-time, so asking to pass a name to my at run-time makes no sense.
Symbolic references can't be used to access lexical variables.
It's a stupid thing to do.
My requirement is as follows. I want to create a dynamic array like this:
my #a0=();
my #a1=();
my #a2=();
my #a3=();
As I have said before, "When you find yourself adding an integer suffix to variable names, think I should have used an array."
So, instead, use
my #data = (
[ ... ],
[ ... ],
[ ... ],
);
If you wanted index each year's data array by the year (instead of integers 0, 1, 2, ..., n), then use a hash:
my %data = (
2005 => [ ... ],
2006 => [ ... ],
2007 => [ ... ],
);
What you have now is compounded stupidity. Don't do that.

Resources