ActionScript 2: How do I determine the number of keys in an associative array without iterating? - actionscript-2

Is there a function that allows me to determine the number of keys in an ActionScript 2 associative array without iterating over that array?
// ascertain the length/size of an associative array
var o:Object = new Object();
o["k1"] = "v1";
o["k2"] = "v2";
o["k3"] = "v3";
I'd expect there to be an "o.size" or "o.length" that would return 3.
Thanks.

var o:Object = new Object();
o["k1"] = "v1";
o["k2"] = "v2";
o["k3"] = "v3";
var len:Number = 0;
for( i in o ) len++;
trace( len );
Sorry, there's no length/size for an Object, iteration is your only choice. AS3 has better options for this with the Dictionary class.

Related

Get array of double from list of array of double, where first value in array is the max first value using linq

I want to get the array of doubles from a list of an array of doubles where the first value of the array is the highest value as compared to the first value of the other arrays in the list
For example, if the list of arrays are (4,5) , (-3,2) , (7,1) then it would return (7,1).
I tried this but no success:
Dim vertices as List(of Double()) = MethodToGetList()
dim rv as Double() = vertices.Select(Function(x As Double()) x.First.MaxValue).ToArray
Sorry, my basic is a bit rusty, I'll do it in C#, I hope you'll get the gist.
Whenever you want a LINQ function, where you need to examine every item of your sequence exactly once, and you want to do something with the item that is currently being examined, think of Aggregate.
This will initialize variable highestValue with your first source element; then it will compare every source element with the highestValue and keep the one that is highest:
List<List<double>> mySource = ...
var result = mySource.Aggregate( (highestValue, nextValue) =>
(nextValue[0] > highestValue[0]) ? nextValue : highestValue);
This will be similar to:
List<double> highestValue = mySource[0];
for (int i=1; i<mySource.Count; ++i)
{
if (mySource[i][0] > higestValue[0])
{
// found a new highestValue
highestValue = mySource[i];
}
// else: keep the highest value
}
It is easy to see, that this only works if mySource is not empty. If you have an empty source, you should think of what you want as a result. We will initialize with this emptyResult:
List<double> emptyResult = ... // what do you want to use if there is nothing in your source?
List<double> highestValue = emptyResult;
for (int i=0; i<mySource.Count; ++i)
{
if (mySource[i][0] > higestValue[0])
{
// found a new highestValue
highestValue = mySource[i];
}
}
And now in LINQ: use the other Aggregate:
var result = mySource.Aggregate(emptyResult, // use this as seeder
(nextValue[0] > highestValue[0]) ? nextValue : highestValue);
This will help if you have an empty sequence. Buf what if one of your Lists in the source is null or empty?
Let's assume you don't want to consider them at all:
var nonEmptyArrays = mySource.Where(list => list != null && list.Count != 0)
var result = noneEmptyArrays.Aggregate((emptyResult, // use this as seeder
(nextValue[0] > highestValue[0]) ? nextValue : highestValue);
Simple comme bonjour!
Try this. This orders the vertices list of arrays by the first position of each array, then by the second position of the array. Then in the case of the same data (below), the list would look like: (7,1), (7,2), .... Then last piece of the statement grabs the first element in the sorted list so would return (7, 1)
Dim rv As Double() = vertices.OrderByDescending(Function(v) v(0)).ThenBy(Function(v) v(1)).FirstOrDefault
Data to test:
Dim vertices As List(Of Double()) = New List(Of Double())
vertices.Add(New Double() {4, 5})
vertices.Add(New Double() {-1, 2})
vertices.Add(New Double() {7, 1})
vertices.Add(New Double() {7, 2})

LINQ Where clause Multiple conditions

Im trying to get all the data rows that contain a certain set of data.
In my database i have some values, including start and length.
I have an array that contains integers called startTimes and another called endTimes.
I need a where clause that would return the records that have a start value contained in startTimes, OR start+length contained in endTimes.
Is there any way to do this?
Thanks
IQueryable<request> proposedRequest = db.requests.Include(r => r.rooms);
proposedRequest = proposedRequest.Where(r=>r.booked.Equals(1));
proposedRequest = proposedRequest.Where(r=>r.roundID.Equals(roundID))8;
proposedRequest = proposedRequest.Where(r=>r.day.Equals(day));
int[] startTimes;
int[] endTimes;
for(var q=0;q<time;q++){
startTimes[startTimes.Length] = time + q;
endTimes[endTimes.Length] = time + q + 1;
}
proposedRequest = proposedRequest.Where(//what goes in here);
i have an array startTime=[1,3,4] and an array endTime=[2,4,5] lets
say. I need to see if any of those values match my records? I dont
think the above would do the job
To check if you have value in Array of int, Use the Contains Method:
proposedRequest = proposedRequest.Where(s => startTimes.Contains(s.INT_ID)
|| endTimes.Contains(s.INT_ID));
Syntax: ARRAY.Contains(ID_TO_SEARCH)
it returns a boolean:
var list = new List<int> {1,2,3,4,5};
var intVar = 4;
var exists = list.Contains(intVar);
will something like this suffice?
var data = collection.Where(
t => startTimes.Contains(t.theTime) ||
endTimes.Contains(t.theTime + theLength));
proposedRequest.Where(pr => startTimesArray.Contains(pr.start) || endTimesArray.Contains(pr.start + pr.length));

Accessing an int[] inside an ArrayList

I'm trying to make a program that reads points (e.g. (2,4), (0,0) ) from a file and tried using ArrayList since I don't know how many many points there will be. However only the last read point seems to get stored. I think only the pointer of nInput[] gets stored and not the actual value hence any subsequent sc.next() only seem to change the existing nInput[] in the ArrayList.
Ex input file:
-1 2
0 1
0 0 <<< I would get this as the only output
OR
-1 2
0 4
9 9 << This would be the only output
int nInput = new int[2];
ArrayList<int[]> aPoints = new ArrayList<int[]>();
while(sc.hasNext()){
nInput[0] = Integer.parseInt(sc.next());
nInput[1] = Integer.parseInt(sc.next());
aPoints.add(nInput);
}
for(int i<0; i<aPoints.size(); i++)
System.out.println(aPoints.get(i)[0]+" "+ aPoints.get(i)[1]);
How do I store int[] into an ArrayList?
Edit: First I gave pseudocode but I think you need Java code:
(I assume sc as a Scanner object)
ArrayList<int[]> list = new ArrayList<int[]>();
while(sc.hasNext) {
int[] array = new int[2];
array[0] = sc.nextInt();
array[1] = sc.nextInt();
list.add(array);
}

zf2 validation form: How can I validate dependent fields?

I have 5 text form
$number1 = new Text('number-1');
$number2 = new Text('number-2');
$number3 = new Text('number-3');
....
with the relative filters
foreach(...)
$input = new Input($elementName);
$validator = new ValidatorChain();
$validator->addByName('Int')
->addByName('Between', array(
'min'=>0,
'max'=>$this->maxAllowedTicket,
'inclusive'=>true));
$filter = new FilterChain();
$filter->attachByName('Int');
$input->setValidatorChain($validator);
$input->setFilterChain($filter);
I would that only one of this 3 forms can contain a value different from 0.
There are then two possible errors.
TOO MANY NUMBERS
$data['number1'] = 5;
$data['number2'] = 5;
$data['number3'] = 0;
$data['number4'] = 5;
$data['number5'] = 0;
NO NUMBER
$data['number1'] = 0;
$data['number2'] = 0;
$data['number3'] = 0;
$data['number4'] = 0;
$data['number5'] = 0;
How can I validate this multiple fields at the same time ?
You need to write your own Validator class to do so. The isValid() method of your new validation class also receives the $context which includes the values of the whole form. This way you can validate the value of each field depending on the other fields.
namespace My\Validator;
use Zend\Validator\AbstractValidator;
class CustomValidator extends AbstractValidator
{
public function isValid($value, $context = null)
{
// put your logic here and call
// $this->error(); if required
}
}
Craft your own solution using the Callback validator.
Examples are here: http://packages.zendframework.com/docs/latest/manual/en/modules/zend.validator.set.html#callback

How to sort a List<dynamic> containing ExpandoObjects

I have a list which contains a dictionary of ExpandoObjects. Im binding this to a grid but now I want to sort the list.
var rows = new List<dynamic>();
for (int i = 0; i < 1000; i++)
{
dynamic expandy = new ExpandoObject();
var dictionary = (IDictionary<string, object>)expandy;
dictionary.Add("ID", i);
dictionary.Add("Name", "Name" + i);
rows.Add(dictionary);
}
So looking at the test code above how would I sort rows (ascending or decending) say on "ID" or "Name" or any other property that I dynamically add?
A bit more info, I'm looking to sort it like this (this doesnt work);
var newOrder = from r in rows
orderby ("Name") ascending
select r;
Not sure how I missed this but anyway this works,
var newOrder = rows.OrderByDescending(x => x["Name"]);
WooHoo's answer does not work for Dynamic.ExpandoObject; this works:
var newOrder = rows.OrderByDescending(x =>((IDictionary<string,object>)x)["Name"]);