Posts

Showing posts with the label arrays

VBA Count occurrence of an element in a multi-dimensional array, how?

VBA Count occurrence of an element in a multi-dimensional array, how? I have defined a multi-dimensional array using range in vba, for example Dim Arr() as Variant Arr = Range("A1:F5") This resulted the a 5x6 array Arr(1,1) to Arr(5,6) I want to count the occurrence of a string, say "ABC" in Arr(5) (i.e. Row 5) only. The following code can find the count of "ABC" in the all of the array For Each x in Arr if x = "ABC" then Cnt = Cnt + 1 Next But if only want to count dimension 5, Arr(5) return an error. Arr is Arr(1 to 5, 1 to 6). Which 5 are you talking about? – Jeeped Jun 30 at 9:22 2 Answers 2 Sub FindABC() Dim Arr(), cnt, x Const ROW_NUM = 5 Arr = Range("A1:F5") For x = 1 To UBound(A...

ArrayList.add not working with AsyncTask

ArrayList.add not working with AsyncTask I'm using fragment view to show ListView in my Android Application. It is working perfectly when I manually enter ArrayList . But now I want to add JSON array to the ArryList in AsyncTask class and add that ArrayList to Adapter. ListView ArrayList ArryList AsyncTask ArrayList So I got JSON array and put It ArrayList . This process doing in the AsyncTask class. ArrayList AsyncTask I execute that AsyncTask class in onCreateView() method in Fragment. Then I set ArrayList to Adapter. But problem is there.. AsyncTask onCreateView() ArrayList After execute AsyncTask class get my JSON array perfectly and add to the ArrayList . But when I try to add my ArrayList to Adapter. Then my ArryaList empty. AsyncTask ArrayList ArrayList ArryaList This my fragment class with AsyncTask Part AsyncTask public class FragmentCategory extends Fragment { private ListView lvCategory; private CategoryAdapter categotyAdapter; private List<Category> mC...

Find the sum of an int array - leaving out all numbers between a 6 and a 7, inclusive

Find the sum of an int array - leaving out all numbers between a 6 and a 7, inclusive I would like to write a program which adds up all the numbers in an integer array - with an exception! Since the number 6 isn't the nicest, I propose that we exclude all sections of numbers beginning with a 6 and ending with a 7 - inclusive. Every 6 will always be followed by a 7 , but not necessarily vice versa. Here are a few examples of the input arrays and their expected output: sum67([1, 2, 2, 6, 99, 99, 7]) = 5 All numbers between a 6 and a 7 are excluded. sum67([1, 2, 2, 6, 99, 99, 7]) = 5 sum67([1, 2, 2]) = 5 No 6's or 7's here. sum67([1, 2, 2]) = 5 sum67([1, 1, 6, 7, 2]) → 4 Neither the 6 or the 7 is included. sum67([1, 1, 6, 7, 2]) → 4 All the above tests passed. Once again, the method header is fixed, and I may only change the body of the method. Here is my attempt at the code: public int sum67(int nums) { int sum = 0; for (int i = 0; i < nums.length; i++) { // Add...

Updating Values in A Multidimensional Cart Array

Updating Values in A Multidimensional Cart Array I am writing a custom shopping cart in PHP, I add product info to the cart session like this: $product_array=array($title,$price,$qty); if(!isset($_SESSION['cart'])) $_SESSION['cart']=array(); if(!in_array($product_array,$_SESSION['cart'])) $_SESSION['cart']=$product_array; else { // update price and quantity here } My challenge: I want to update the price and quantity($qty) of the product if it already exists in $_SESSION['cart'] array rather than adding it. Something like this price = price + $price, qty = qty + $qty 1 Answer 1 This example is similar to your example. you can add code from foreach loop in else condition. I am considering product_id instead of $title variable. $_SESSION['cart'] = [ 'product_id'=> 12, 'price' => 100 , 'quantity' => 2 ]; $_SESSION['...

Sort isues with TArray for large numbers

Sort isues with TArray<Myrectype> for large numbers What is the reason why TArray.Sort does not work when I have large numbers in the comparison ? My code is as follows (Delphiy Tokyo): Interface Type RCInd = record Num : Integer; Ger : Integer; Confirmed : Boolean; Total : Real; End; TArrInd = TArray<RCInd>; Procedure SortInd (Var PArrayInd : TArrInd); Implementation Procedure SortInd (Var PArrayInd : TArrInd); begin TArray.Sort<RCInd>( PArrayInd,TComparer<RCInd>.Construct ( function (Const Rec1, Rec2 : RCInd) : Integer begin Result := - ( Trunc(Rec1.Total) - Trunc(Rec2.Total) ); end ) ); end; ...... When the values of Rec1.Total and Rec2.Total are whithin a Integer limits , this Sort works fine , BUT when values exceed Int...

How can I find closest date value from an array according to the current date?

How can I find closest date value from an array according to the current date? I want to get the next closest date on this array according to the current date. var dates = [ 'Aug 18, 2018 03:24:00', 'August 19, 2018 03:24:00', 'September 17, 2018 03:24:00', 'September 14, 2018 03:24:00', 'August 18, 2018 03:24:00', 'July 16, 2018 03:24:00', 'July 15, 2018 03:24:00', 'December 15, 2018 03:24:00', 'July 13, 2018 03:24:00', ]; var now = new Date(); 4 Answers 4 First you need to convert each date into a timestamp then subtract each of them by the current date and store the timestamp difference in the temp array then get the index of the minimum value and use the index to access the closest date in the original array. var dates = [ 'July 16, 1995 03:24:00', 'Aug 18, 1995 03:...