I want to slice an array to create a new array containing all but the first entry in the array.
This is what i tried:
#arguments = $splittedCommands[1..-1];
Which gives me an empty result.
1 should be the second entry and -1 should be the last, so why doesn't this work?
You should use # in front of array when slicing an array ($ tells that your're accessing single scalar value inside array), so
my #arguments = #splittedCommands[ 1 .. $#splittedCommands ];
Second, your range should be either -$#splittedCommands .. -1 or 1 .. $#splittedCommands with later being more common and straightforward.
Easiest may be to assign it into another list, using
( undef, my #commands ) = #splittedCommands;
Assigning into undef here throws away the first result, then the remainder goes into #commands
Another approach could be to assign the lot and then remove the first
my #commands = #splittedCommands;
shift #commands;
This could also be simplified if you didn't need to keep the original array around any more; just shift the first item off #splittedCommands then use it directly.
If the original array must be left intact then the obvious way seems to be to copy the whole array and remove the first element.
shift(my #commands = #splittedCommands)
will do just fine
Related
I want to change a single element in a vector:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
input_vecteur is a vector of size 6001 -by- 1 filled with the same value: 61.46 , and vecteur_avec_delai = ones(6001,1)
I want to change one value of vecteur_avec_delai to 61.46, so to still have a vector of length 6001, filled with ones except one cell with a value equal to 61.46, thus the vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
When I run :
vecteur_avec_delai=delai(input_vecteur, iteration)
It compiles but gives me for output a vecteur_avec_delai of size (iteration, 1 ) filled with 0s except for the last value equal to 61.46.
When I try this operation "manually" (directly in the MATLAB command window), it works, so why doesn't it when I go through this function?
You haven't declared vecteur_avec_delai as input into the function. This boils down to calling A(10,1) = 1 in a clean command window: it creates a 10-by-1 vector with 9 zeros and a 1 at the end. Instead, first declare your array to be ones and then set only this required element:
function vecteur_avec_delai=delai(input_vecteur, iteration)
vecteur_avec_delai = ones(size(input_vecteur));
vecteur_avec_delai(iteration,1) = input_vecteur(iteration,1);
end
Or, given your comment about it working in the command window, simply add vecteur_avec_delai to the inputs of the function (you might want to change the names of the input and output variables to not be the same for clarity). Functions are closed name-spaces, i.e. they have their own "workspace" so to say, and cannot see anything outside of that function, hence the need to declare all inputs.
Lets say I have the following:
nested_object = [0, 1, 2, {foo: 'bar'}]
How do I use dig to select the last element of the array as I search for deeper nested objects?
A negative value integer in any dig method arguments starts at the end of the array and moves towards the beginning. So -1 selects the last element of the array, -2 selects the second from last, and so on. So, in your case:
target = nested_object.dig(-1, :foo)
will select the last element of the array and proceed from there.
using Perl I am trying to push the elements of an array to another array, and not the whole array. But I'm not getting my goal.
I tried this:
push #tmp_entities_all, #tmp_entities;
But I got the whole small array as an element in the bigger array.
Then I tried it with a loop:
for (#tmp_entities) {push #tmp_entities_all, $_;}
But the same results, the whole #tmp_entities appears as an element, and that what I dont want.
I need one dimension in the array and not array of arrays!! Should I cast something before pushing? or what is the problem?
Thanx a lot.
Out of your comments, its obvious, that #tmp_entities contains only one element, that is an array reference to the elements that you expected to be elements of #tmp_entities.
So you perhaps declared your array with an array refence instead of using a set of elements.
The line
push #tmp_entities_all, #tmp_entities;
definitly works for a normal array.
In your case, your could try ...
push #tmp_entities_all, $tmp_entities[0];
or you simply try to initialize your array with its value like
my #tmp_entities = ( 1, 2, 3 ); # initialize array with 3 elements of type int
instead of
my #tmp_entities = [ 1, 2, 3 ]; # initialize array with 1 element that is an array reference with 3 elements of type int
I know, that this is the case, because this is why your for-loop sample works with #$_ ;D (it is equivalent to push #tmp_entities_all, $tmp_entities[0]; in this situation).
I have a array called 'BODY_STAT_ARRAY' and in this array it holds a object called 'body_stat'. This array needs to increase a decrease in size all the time, how do i remove the last object from the array, here is a example which doesn't work
if(BODY_STAT_ARRAY.length > target_size)
{
BODY_STAT_ARRAY.slice(BODY_STAT_ARRAY.indexOf(BODY_STAT_ARRAY[BODY_STAT_ARRAY.length-1]),1)
// this line above should remove the last object in the array
}
So where am I going wrong, how do you make it work.
If you can help, I would love to find out.
The easiest way to remove the last object from an array is to call pop:
BODY_STAT_ARRAY.pop();
Slice will not modify the Original Array . It will give an another Array with the defined range .
Only Splice will modify the original Array , so as to remove or add elements .
var a:Array = [1,2,3,4];
a.slice(1,1);
trace(a); // Output is : 1,2,3,4
a.splice(1,1);
trace(a); // Output is : 1,3,4
I'm a total Perl newb, but still cannot believe I cannot figure this out with all the info I've read through online, but, I've burned too much time and am suffering from block at this point. Hoping to learn something based on my real life example...
Ok, I think I have an array of arrays, created like this:
my #array1 = ();
my #array2 = ();
my $ctr1 = 0;
my $col;
[sql query]
while(($col)=$sth->fetchrow_array() ) {
$array1[$ctr1]=$col;
$ctr1++;
}
print STDERR "#array1";
##results in 10 rows, a mac address in each
##00:00:00:00:00:00 00:11:11:11:11:11 22:22:22:22:22:22 33:33:33:33:33:33 ...
Now I do another query. While looping through results, I am looking for those 10 mac addresses. When I find one, I add a row to array2 with the mac and the sequential number accumulated to the point, like this:
[sql query]
while(($col)=$sth->fetchrow_array() ) {
$ctr2++;
if( my ($matched) = grep $_ eq $col, #array1 ) {
push( #array2, ($col,$ctr2) );
}
}
print STDERR "#array2";
##results in 10 rows, a mac address and an integer in each
##00:00:00:00:00:00 2 00:11:11:11:11:11 24 22:22:22:22:22:22 69 33:33:33:33:33:33 82 ...
Now the easy part. I want to loop through array2, grabbing the mac address to use as part of a sql query. Therein lies the problem. I am so ignorant as to exactly what I am doing that even though I had it almost working, I can't get back to that point. Ignorance is definitely not bliss.
When I loop through array2, I am getting a host of errors, based on the different forms of the statement. The one I think is right is listed below along with the error message...
my $ctr3 = 0;
foreach $ctr3 (#array2) {
my $chkmac = $array2[$ctr3][0]; <--- gacks here with the error below - line 607
[SQL query]
[Thu May 30 14:05:09 2013] [error] Can't use string ("00:66:55:77:99:88") as an ARRAY ref while "strict refs" in use at /path/to/test.cgi line 607.\n
I believe the issue is that my array of arrays is not an array of arrays. If it were, it would work as coded, or so I think from the reading... That said, I cannot fathom what I am dealing with otherwise. This will be a head slapper I'm all but sure, but I am stumped.... Little help, please?
TIA
O
For an array of arrays you want to use an array reference, e.g.
push #array2, [$col, $ctr2];
When accessing an element within an array refernce, you'll want to use the -> operator. Also, when looping through an array, it's not necessary to index back into that same array. So the last part would look more like:
foreach $ctr3 (#array2) {
my $chkmac = $ctr3->[0];
....
When you do the foreach there, $ctrl3 won't have the index in it, it'll have the value. So you should just need to do $ctrl3->[0]. Note the -> which dereferences the array reference (#array2 is actually an array of array references).
EDIT: As AKHolland pointed out, #array2 actually isn't an array of array references, although that's what it should be. You also need to change:
push( #array2, ($col, $ctr2) );
To
push( #array2, [$col, $ctr2] );
This makes an array reference, rather than a list. A list in this context just collapses down into regular arguments to push, meaning you're pushing two separate strings into #array2.
You are correct that your array of arrays is not an array of arrays, since in Perl there is no such thing. So what do you have instead? There's two ways to see.
First, when you print #array2, you come up with a string composed of alternating MACs and counts, separated by spaces. Since the spaces sort-of-signify the division between array elements, we might surmise that what we've got is a single array of heterogeneous elements, such that element 0 is a MAC, element 1 is a count, element 2 is another MAC, and so on.
The other perspective is to look at how #array2 is constructed:
push( #array2, ($col,$ctr2) );
From the documentation for push, we find that push ARRAY LIST works by appending the elements of LIST to the end of ARRAY. This has the effect of flattening the list into the array such that its original identity as a list is lost. You can add all the parentheses you want, when Perl expects a list it flattens all of them away.
So how do you achieve the effect you want? The List-of-Lists documentation has a detailed treatment, but the short answer is that you make a list of array references. Array references are scalars and are therefore legal elements in an array. But they retain their identify as array references!
The anonymous array reference constructor is the square bracket []. In order to push an array reference containing the elements $col and $ctr2 onto the end of #array2, you simply do this:
push( #array2, [$col, $ctr2] );
The code you wrote for accessing a particular element of the array-reference-in-an-array now works. But since I've already written a bunch of paragraphs on the subject, let me finish by explaining what was wrong originally and how changing the push statements suddenly makes it work.
The expression $array2[$ctr3][0] is sometimes written as $array2[$ctr3]->[0] to clarify what it's actually doing. What it does is to take the value of $array2[$ctr3] and treat it as an array reference, taking its 0 element. If we take $ctr3 to be 0 (as it would be at the top of the loop) the value of $array2[$ctr3] is the first element, 00:00:00:00:00:00. When you then subsequently ask Perl to treat 00:00:00:00:00:00 as an array reference, Perl dies because it doesn't know how to treat a string as an array reference.
When instead the value of $array2[$ctr3] is an array reference because that is what you pushed onto #array2 when constructing it, Perl is able to do as you ask, dereferencing the array reference and looking at element 0 of the resulting array, whose value happens to be 00:00:00:00:00:00.