CoffeeScript - remove an item once pushed - coffeescript

How can I remove an element from the array which was pushed in with push?
animals = []
class Cat
cat = new Cat
animals.push cat
now can I say something like
animals.pull cat
to take the cat away from the array? Presuming that the cat variable is the same one that was pushed inside. I am just trying to create a kind of a dynamic collection..

index = animals.indexOf cat
animals.splice index, 1 if index isnt -1
Remember, a CoffeeScript array is just a JavaScript array, so you can look at any appropriate documentation.

If you push an element to an array, it's added at the last position. You can then pop it back. These two methods are useful if you want to model a stack with an array.
array = []
array.push 'hello' # array is now ['hello']
array.push 'world' # array is now ['hello', 'world']
alert array.pop() # alerts 'world', array is now ['hello']
alert array.pop() # alerts 'hello', array is now []

You could do:
animals = []
class Cat
cat = new Cat
animals.push cat
anotherReferenceToCat = animals.pop()
# animals.length === 0

Related

How do I declare a list in YAML and read using PERL YAML::XS

I can create a value in YAML as such:
MYVAL: 1
I can load this in my PERL as follows:
my $settings = YAML::XS::LoadFile...
my $number_mine = $settings->{'MYVAL'};
I would want to create now an array of strings in YAML.
I tried using - and --- but not seeing it
YAML?
MYARRAY: str1,str2,str3
PERL:
my #array_mine = $settings->{'MYARRAY'};
This:
MYARRAY: str1,str2,str3
is a YAML mapping, the same way as your
MYVAL: 1
is a YAML mapping. The difference is that the value for the key MYARRAY is a plain (i.e. non quoted) scalar string str1,str2,str3 and for the value MYVAL is the scalar integer 1
If you want a sequence of three strings as value on a single line, you would need to do:
MYARRAY: [str1,str2,str3]
(optionally with whitespace before and/or after the commas). That is a flow style sequence of three plain scalars: str1, str2 and str3.
An alternative is to use block style:
MYARRAY:
- str1
- str2
- str3
which is semantically equivalent to the flow style example above.
Dump out a list and see what it looks like:
$ perl -MYAML -E 'say YAML::Dump( { MYARRAY => ["str1","str2","str3"] })'
---
MYARRAY:
- str1
- str2
- str3

MATLAB - Is there a way to do a string compare using multiple strings

I have a list of strings that I am looking for in a pdb
X1 = ['N' 'CA' 'CB' 'CG1']%Isoleucine
I want to compare these strings to the atoms as so:
atoms = find(strcmp({pdb.Model.Atom(:).resName}, 'ILE') & ...
(strcmp({pdb.Model.Atom(:).AtomName}, 'N') |...
strcmp({pdb.Model.Atom(:).AtomName}, 'CA') | ...
strcmp({pdb.Model.Atom(:).AtomName}, 'CB') | ...
strcmp({pdb.Model.Atom(:).AtomName}, 'CG1')))
Is there a more concise way to do this? Also is there a way for strcmp to do an exact match? not just if it contains the string?
EDIT:
A more concrete example:
I want to be able to do this:
strcmp(['hello' 'world'], ['hello' 'world' 'this' 'is' 'a' 'test'])
and it returns whether it matched with hello or world. This instead returns a zero saying that it can't find this array ['hello' 'world'] in ['hello' 'world' 'this' 'is' 'a' 'test'].
Essentially I just want to know if 'hello' and 'world' are in the larger array instead of checking if it contains both 'hello' and 'world' in the same row
The first half of your question makes me uncertain, but the second formulation is easily answerable:
>> ismember({'hello' 'world'}, {'hello' 'world' 'this' 'is' 'a' 'test'})
ans =
1 1
As you can see, ismember works for cell arrays (which is the general container for strings). So you can probably use all to check the presence of each of the strings in the larger collection:
all(ismember({pdb.Model.Atom(:).AtomName},{'N','CA','CB','CG1'}))

How to display selected entries of an array of structures in MATLAB

Suppose we have an array of structure. The structure has fields: name, price and cost.
Suppose the array A has size n x 1. If I'd like to display the names of the 1st, 3rd and the 4th structure, I can use the command:
A([1,3,4]).name
The problem is that it prints the following thing on screen:
ans =
name_of_item_1
ans =
name_of_item_3
ans =
name_of_item
How can I remove those ans = things? I tried:
disp(A([1,3,4]).name);
only to get an error/warning.
By doing A([1,3,4]).name, you are returning a comma-separated list. This is equivalent to typing in the following in the MATLAB command prompt:
>> A(1).name, A(3).name, A(4).name
That's why you'll see the MATLAB command prompt give you ans = ... three times.
If you want to display all of the strings together, consider using strjoin to join all of the names together and we can separate the names by a comma. To do this, you'll have to place all of these in a cell array. Let's call this cell array names. As such, if we did this:
names = {A([1,3,4]).name};
This is the same as doing:
names = {A(1).name, A(3).name, A(4).name};
This will create a 1 x 3 cell array of names and we can use these names to join them together by separating them with a comma and a space:
names = {A([1,3,4]).name};
out = strjoin(names, ', ');
You can then show what this final string looks like:
disp(out);
You can use:
[A([1,3,4]).name]
which will, however, concatenate all of the names into a single string.
The better way is to make a cell array using:
{ A([1,3,4]).name }

Why does my value change when I am not resetting it?

I have the following example exhibiting the problem I'm struggling to resolve.
In the toy example, I have an array #actors with two levels.
I also have an array of hashes #people which I am using to 'look up' properties of the people in #actors.
The output of the program should be:
blue, blue cat, cat
red, red dog, dog
blue, blue cat, cat
red, red dog, dog
but instead I'm getting:
blue, cat cat, cat
red, dog dog, dog
blue, cat cat, cat
red, dog dog, dog
That is, it seems that in setting $favanim[$i][$j] I seem to be also overwriting the value of $favcols[$i][$j].
I suspect that for some reason the fact that #actors is a 2-dimensional array means that assignments via = are as references rather than as values, though I don't know why or how to stop it.
Please help!
The toy program is here: (I apologise if it can be simplified whilst still exhibiting the problem - it has taken me most of the afternoon to strip it down to this)
#!/usr/bin/perl -w
my #people = ();
$people[0]{'alternative full names for regexp'} = 'matthew smith|matt smith';
$people[1]{'alternative full names for regexp'} = 'david tennant|dave tennant';
$people[0]{'fav colour'} = 'red';
$people[1]{'fav colour'} = 'blue';
$people[0]{'fav animal'} = 'dog';
$people[1]{'fav animal'} = 'cat';
my #actors = ();
$actors[0][0] = 'David Tennant';
$actors[0][1] = 'Matt Smith';
$actors[1][0] = 'David Tennant';
$actors[1][1] = 'Matt Smith';
my #favcols = #actors;
my #favanim = #actors;
for ($i=0; $i<2; $i++) {
for ($j=0; $j<2; $j++) {
my #matching_people = grep{$actors[$i][$j] =~ m/^$_->{'alternative full names for regexp'}$/i} #people;
$favcols[$i][$j] = $matching_people[0]{'fav colour'};
$favanim[$i][$j] = $matching_people[0]{'fav animal'};
print "$matching_people[0]{'fav colour'}, $favcols[$i][$j] $matching_people[0]{'fav animal'}, $favanim[$i][$j]\n";
}
}
Try using
#favcols = map { [#$_] } #actors;
#favanim = map { [#$_] } #actors;
Deep copy vs shallow copy.
The problem is that you are initialising #favcols and #favanim by copying the contents of #people, which contains two array references
That sets $favcol[0] and $favanim[0] to a reference to the same array, [ 'David Tennant', 'Matt Smith' ], so when you modify $favcols[$i][$j] and then $favanim[$i][$j] you are overwriting the same array element
I don't see any reason to initialise your arrays at all, and if you declare them as just
my (#favcols, #favanim);
then you will find that your program does what you expect
By the way, you must always use strict, and use warnings is preferable to -w on the command line

Need to copy only a portion of array string to another array

This is my source array:
my #raw_stack = (
'a1~a2~a3~a4~a5',
'b1~b2~b3~b4~b5',
'c1~c2~c3~c4~c5',
'd1~d2~d3~d4~d5',
'e1~e2~e3~e4~e5',
);
I want get the 3rd value in '~' pattern then place that
to another array.
The other array should now look like this:
my #other_stack = (
'a3',
'b3',
'c3',
'd3',
'e3',
);
I could go about looping through the stack array then split
and push to another array, but i'm looking for a lean way
to code this.
Any ideas?
my #other_stack = map {(split/~/)[2]} #raw_stack;
Use map to list transform instead of push. Use index/substr or unpack if the items are fixed-width. This generally is faster than split, which uses regex.