Tag Archive for arrays

Insert a value into a numerically-ordered array in the correct order

def insert_value_in_order(array_of_values, new_value, update_array = nil)
	value_index = array_of_values.index(new_value)
	index = 0
	if !value_index && array_of_values.last.to_i > new_value
		i = 0
		while array_of_values[i].to_i < new_value do
			i += 1
		end
		index = i
	else
		index = value_index ? value_index : -1
	end
	return update_array == true || update_array == 1 ? array_of_values.insert(index, new_value) : index
end

source

Are Arrays Equal

function areArraysEqual(byval array1, byval array2)
	areTheyEqual = true
	if ubound(array1) <> ubound(array2) then
		areTheyEqual = false
	else
		for i = 0 to ubound(array1)
			if (array1(i) <> array2(i)) then
				areTheyEqual = false
			end if
		next
	end if
	areArraysEqual = areTheyEqual
end function

source

Performance in Array Processing

//Copyright 2009 Nicholas C. Zakas. All rights reserved.
//MIT Licensed
function timedChunk(items, process, context, callback){
var todo = items.concat();   //create a clone of the original

setTimeout(function(){

var start = +new Date();

do {
process.call(context, todo.shift());
} while (todo.length > 0 && (+new Date() - start < 50));

if (todo.length > 0){
setTimeout(arguments.callee, 25);
} else {
callback(items);
}
}, 25);
}

source

Array in C#

using System;
using System.Collections.Generic;
using System.Text;
namespace ArraysInCSharp
{
class Program
{
static void Main(string[] args)
{
SingleDim();
Console.WriteLine("++++++++++++++++++");
MultiDim();
}

static void SingleDim()
{
string[] strCities = new string[4] { "Houston", "Austin", "Tuscon", "Sedona" };

foreach (string str in strCities)
{
Console.WriteLine(str);
}
}

static void MultiDim()
{
string[,] strCityStates = new string[,]
{ { "Houston", "TX" }, { "Austin", "TX" }, { "Tuscon", "AZ" }, { "Sedona", "AZ" } };

Console.WriteLine(strCityStates[0,0] + "," + strCityStates[0,1]);
Console.WriteLine(strCityStates[1,0] + "," + strCityStates[1,1]);
Console.WriteLine(strCityStates[2,0] + "," + strCityStates[2,1]);
Console.WriteLine(strCityStates[3,0] + "," + strCityStates[3,1]);
Console.WriteLine(" ");

foreach (string str in strCityStates)
{
Console.WriteLine(str);
}
}
}
}

source

How to detect missing values using Arrays

Question:
How do I exclude observations from my PROC FREQ analysis when a value is missing from a list of variables?

Answer:
In the SAS DATA step, you can create a new variable ("miss" in the example below) that is set equal to 1 when a variable has a missing value, 0 otherwise. Use the ARRAY statement and a DO loop to check for missing values across a list of variables; the syntax is:

read more at...

<a href="http://studysas.blogspot.com/2008/11/how-to-detect-missing-values-using.html

" >http://studysas.blogspot.com/2008/11/how-to-detect-missing-values-using.html
source

Convert columns into rows or rows into columns

During my early days as a SAS programmer, I get confused very easily with PROC TRANSPOSE. I mean, I get confused with what are the variables that I need to include in BY statement and ID statement as well as in VAR statement.

PROC TRANSPOSE syntax looks like a straightforward thing, but we need to look at important details it offers and without that we may get unexpected results.

source

Array Merge

<%
' Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
'
' This work is licensed under the Creative Commons Attribution License. To view
' a copy of this license, visit <a href="http://creativecommons.org/licenses/by/3.0/" >http://creativecommons.org/licenses/by/3.0/</a> or
' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
' 94305, USA.

' Merge two arrays into one.
function array_merge(byVal firstArray, byVal secondArray)
dim totalSize
dim i
dim combinedArray

' Ensure that we're dealing with arrays.
if not isArray(firstArray) then
firstArray = Array(firstArray)
end if

if not isArray(secondArray) then
secondArray = Array(secondArray)
end if

' Set up the new array.
totalSize = uBound(firstArray) + uBound(secondArray) + 1
combinedArray = firstArray
redim preserve combinedArray(totalSize)

for i = 0 to uBound(secondArray)
combinedArray(uBound(firstArray) + 1 + i) = secondArray(i)
next

array_merge = combinedArray
end function
%>

source

list (array) of months (spanish)

$GLOBALS['months_es'] = array(1 => 'Enero', 2 => 'Febrero', 3 => 'Marzo', 4 => 'Abril', 5 => 'Mayo', 6 => 'Junio', 7 => 'Julio', 8 => 'Agosto', 9 => 'Septiembre', 10 => 'Octubre', 11 => 'Noviembre', 12 => 'Diciembre');

source

Arrays in BASH

### Arrays en bash ###

## Declarar un array
# 1 - Con typeset o declare
typeset ARRAY[10] # Definido un array de nombre ARRAY de 10 elementos
typeset -a VECTOR # Definido un array de nombre VECTOR sin tamaño definid
decalre ARRAY[10] # Definido un array de nombre ARRAY de 10 elementos
declare -a VECTOR # Definido un array de nombre VECTOR sin tamaño definid

# 2 - Declarando y asignando
LIST[0]="Tomate"  # Se crea un array LIST cuyo primer elemento es "tomate"
NAMES=(Pepe Ana Juan Antonio María)  # Se crea una lista yle asigna los nombre.

## Asignar valores
# NOMBRE_ARRAY[indice]=Valor
LIST[1]="Zanahoria"

## Mostrar valores
# Para un valor concreto: ${NOMBRE_ARRAY[indice]}
echo ${LIST[0]}
# -> Tomate
echo ${LIST[1]}
# -> Zanahoria
# Para mostrar todos los valores:
# - ${NOMBRE_ARRAY[*]} -> Devuelve una cadena con todos los valores
# - ${NOMBRE_ARRAY[@]} -> Devuelve una cadena por valor
echo ${NAMES[*]}
# -> Pepe Ana Juan Antonio María
echo ${NAMES[@]}
# -> Pepe Ana Juan Antonio María
for i in  "${NAMES[*]}"; do echo $i ; done
# -> Pepe Ana Juan Antonio María
for i in  "${NAMES[@]}"; do echo $i ; done
# -> Pepe
# -> Ana
# -> Juan
# -> Antonio
# -> Marí

## Tamaño del array
# ${#NOMBRE_ARRAY[*]}
NAMES=(Pepe Ana Juan Antonio María)
echo ${NAMES[*]}
# -> Pepe Ana Juan Antonio María
echo ${#NAMES[@]}
# -> 5

source

Sort array

Public Function SortArray(ByRef TheArray As Variant)
Dim Temp As Variant, X As Integer

Sorted = False
Do While Not Sorted
Sorted = True
For X = 0 To UBound(TheArray) - 1
If TheArray(X) > TheArray(X + 1) Then
Temp = TheArray(X + 1)
TheArray(X + 1) = TheArray(X)
TheArray(X) = Temp
Sorted = False
End If
Next X
Loop
End Function

source