Can I pass arrays to functions just as I would do with primitives such as int and bool?
Can I pass them by value?
How does the function know of the size of the array it is passed?
What would be the best way to extract a column out of mysql results set? My result set is like:
[0] = ('id'=>1, 'name'=>a),
[1] = ('id'=>2, 'name'=>b),
[2] = ('id'=>3, 'name'=>c)
How can I extract 'name' column as an array from this? I can do it using for loop. But wondering if there are any better reusable solution?
Is there any efficient way in python to count the times an array of numbers is between certain intervals? the number of intervals i will be using may get quite large
like:
mylist = [4,4,1,18,2,15,6,14,2,16,2,17,12,3,12,4,15,5,17]
some function(mylist, startpoints):
# startpoints = [0,10,20]
count values in range [0,9]
count values in range [10-19]
output = [9,10]
i need a solution for array_replace_recursive, because my php-version isnt high enough.
i wat to use this code
$_GET = array_replace_recursive($_GET, array("amp"="&"));
easy, isnt it?
Glubber
If I understood properly you can add value to an array by using :
$myArray[] = 123;
or
array_push($myArray, 123);
Is one cleaner/faster then the other one ?
I just wonder how array_diff() works.And obviously it couldn't work as follows
function array_diff($arraya, $arrayb)
{
$diffs = array();
foreach ($arraya as $keya => $valuea)
{
foreach ($arrayb as $valueb)
{
if ($valuea == $valueb)
{
break;
}
$diffs[$keya]=$valuea;
}
}
return $diffs;
} //couldn't be worse than this
Hope someone can show me some better solution.
Thanks.
Hello, how I can use array acces with my static class?
F.e. I like to execute next script:
class A {
...
}
A['p'] = 15;
echo isset(A['p']) ? A['p'] : 0;
Here is a piece of code that shows some very peculiar performance. For some strange reason, sorting the data miraculously speeds up the code by almost 6x:
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// generate data
const unsigned arraySize = 32768;
int data[arraySize];
for (unsigned c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;
// !!! with this, the next loop runs faster
std::sort(data, data + arraySize);
// test
clock_t start = clock();
long long sum = 0;
for (unsigned i = 0; i < 100000; ++i)
{
// primary loop
for (unsigned c = 0; c < arraySize; ++c)
{
if (data[c] >= 128)
sum += data[c];
}
}
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
std::cout << elapsedTime << std::endl;
std::cout << "sum = " << sum << std::endl;
}
Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds.
With the sorted data, the code runs in 1.93 seconds.
Initially I thought this might be just a language or compiler anomaly. So I tried it Java...
import java.util.Arrays;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
// generate data
int arraySize = 32768;
int data[] = new int[arraySize];
Random rnd = new Random(0);
for (int c = 0; c < arraySize; ++c)
data[c] = rnd.nextInt() % 256;
// !!! with this, the next loop runs faster
Arrays.sort(data);
// test
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < 100000; ++i)
{
// primary loop
for (int c = 0; c < arraySize; ++c)
{
if (data[c] >= 128)
sum += data[c];
}
}
System.out.println((System.nanoTime() - start) / 1000000000.0);
System.out.println("sum = " + sum);
}
}
with a similar but less extreme result.
My first thought was that sorting brings the data into cache, but my next thought was how silly that is because the array was just generated.
What is going on? Why is a sorted array faster than an unsorted array? The code is summing up some independent terms, the order should not matter.
Given the following interface
public interface ISomething {
void DoMany(string[] strs);
void DoManyRef(ref string[] strs);
}
I would like to verify that the DoManyRef method is called, and passed any string array as the strs parameter. The following test fails:
public void CanVerifyMethodsWithArrayRefParameter() {
var a = new Mock<ISomething>().Object;
var strs = new string[0];
a.DoManyRef(ref strs);
var other = It.IsAny<string[]>();
Mock.Get(a).Verify(t => t.DoManyRef(ref other));
}
While the following not requiring the array passed by reference passes:
public void CanVerifyMethodsWithArrayParameter() {
var a = new Mock<ISomething>().Object;
a.DoMany(new[] { "a", "b" });
Mock.Get(a).Verify(t => t.DoMany(It.IsAny<string[]>()));
}
I am not able to change the interface to eliminate the by reference requirement.
Hello all,
I've built a small activex control using c#. I've got a function that returns an array of bytes. From some reason when I try to consue the returned array in js I receive an undefined value. Why is this happenning? Is there anyway to solve it?
Here's a simple demonstration of my code:
Activex:
[ComVisible(true)]
public byte[] Close()
{
try
{
MessageBox.Show("called from activex Close");
return Stop();
}
catch (Exception e)
{
//ExceptionHandling.AppException(e);
throw e;
}
}
Javascript Call:
function CloseActiveX(){
var myRslt = document.OurActiveX.Close();
}
his question is about converting between a struct and a byte array. Many solutions are based around GCHandle.Alloc() and Marshal.StructureToPtr(). The problem is these calls generate garbage. For example, under Windows CE 6 R3 about 400 bytes of garbarge is made with a small structure. If the code below could be made to work the solution could be considered cleaner. It appears the sizeof() happens too late in the compile to work.
public struct Data
{
public double a;
public int b;
public double c;
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataWrapper
{
private static readonly int val = sizeof(Data);
[FieldOffset(0)]
public fixed byte Arr[val]; // "fixed" is to embed array instead of ref
[FieldOffset(0)]
public Data; // based on a C++ union
}
A previous programmer preferred to generate large lookup tables (arrays of constants) to save runtime CPU cycles rather than calculating values on the fly. He did this by creating custom Visual C++ projects that were unique for each individual lookup table... which generate array files that are then #included into a completely separate ANSI-C micro-controller (Renesas) project.
This approach is fine for his original calculation assumptions, but has become tedious when the input parameters need to be modified, requiring me to recompile all of the Visual C++ projects and re-import those files into the ANSI-C project. What I would like to do is port the Visual C++ source directly into the ANSI-C microcontroller project and let the compiler create the array tables.
So, my question is: Can ANSI-C compilers compute and generate lookup arrays during compile time? And if so, how should I go about it?
Thanks in advance for your help!
Here is what I'm trying:
typedef cli::array<int> intarray;
int main(){
intarray ^ints = gcnew intarray { 0, 1, 2, 3 };
intarray::Reverse(ints); // C2825, C2039, C3149
return 0;
}
Compilation resulted in the following errors:
.\ints.cpp(46) : error C2825: 'intarray': must be a class or namespace when followed by '::'
.\ints.cpp(46) : error C2039: 'Reverse' : is not a member of '`global namespace''
.\ints.cpp(46) : error C3149: 'cli::array<Type>' : cannot use this type here without a top-level '^'
with
[
Type=int
]
Am I doing something wrong here?
Hey, guys, I am just starting to wrap my head around objective C and I am doing a little project on Iphone. And I just encountered a weird problem. I had to deal with images in my program so I have a lot local variables declared like temp[width][height]. If I am not using NSThread to perform image processing, it works all fine. However, if I use NSThread, it'll keep giving me EXC_BAD_ACCESS on whenever I try to access a 2-D array declared like temp[widht][height]. So I have to allocate memory from heap in order to have a 2-D array. That'll solve the problem but I still don't get it. My first thought would be stack over flow, but it worked all fine with one thread. I just don't get it.
i have this pointer to 2d array of Robot class
Robot ***rob;
and this is here the code for the constructor !! and the program works fine !!!
but now i am trying to build a destructor to delete this pointer !! and it keeps on crashing the program !!
my question is , how to delete this pointer to 2d array of robots ?
RobotsWorld::RobotsWorld(int x , int y)
{
X=x;Y=y; // returns the limitation of the matrix
rob = new Robot**[x];
for(int i = 0; i < x; i++)
{
rob[i] = new Robot*[y];
for(int j = 0; j < y; j++)
{
rob[i][j] = NULL;
}
}
}
Hi all,
I'm trying to create a color picker for Android that looks like a minimalistic version of Gimp's. So, it has a hue slider and a rectangle with saturation/value variants of a color chosen in hue slider.
Question: what is the best way to create the rectangle?
Right now, I'm creating an 200x200 array of pixels, but it takes ~5sec to create and display rectangle with that array. And I need colors in rectangle to change whenever I change the value in hue slider...
Rectangle is bitmap, btw. Can I use color matrices on that and how? Any examples?
Thanks in advance!
I'm trying to fill an array with words inputted by user. Each word must be one letter longer than previous and one letter shorter than next one. Their length is equal to table row index, counting from 2. Words will finally create a one sided pyramid, like :
A
AB
ABC
ABCD
Scanner sc = new Scanner(System.in);
System.out.println("Give the height of array: ");
String[] words = new String[height];
for(int i=2; i<height+2; i++){
System.out.println("Give word with "+i+" letters.");
words[i-2] = sc.next();
while( words[i-2].length()>i-2 || words[i-2].length()<words[i-3].length() ){
words[i-2] = sc.next();
}
}
Currently the while loop doesn't influence scanner at all :/
Sorry for the novice syntax question.
How do how create an array of channels in go?
var c0 chan int = make(chan int);
var c1 chan int = make(chan int);
var c2 chan int = make(chan int);
var c3 chan int = make(chan int);
var c4 chan int = make(chan int);
That is, replacing the above five lines in one array of channels of size 5?
Many thanks.
Hi All
I want to read in a csv file, filter it based on the values of two of
the fields and set the value of another field. Here's a simple example
of what I'm trying to achieve:
c:\somefile.csv contents:
firstField,secondField,thirdField
1,2,"somevalue"
2,2,"avalue"
3,1,"somevalue"
#Import file into array
$csv = Import-Csv c:\somefile.csv
# Where secondField = 2 and thirdField = "someValue" set thirdField =
"anotherValue"
$csv | where {$_secondField -eq 2 -and $_.thirdField = "somevalue"} |
<set value of thirdField = "anotherValue">
How can I do this. As you can see, from the example about, I can read
in and filter the array. But I don't know how to then set the value of
the thirdField. I tried set-itemproperty but got the error: "The
WriteObject and WriteError methods cannot be called after the pipeline
has been closed".
Any advice on how to achive this would be appreciated.
Alan T
Probably a really simple one this - I'm starting out with C# and need to add values to an array, for example:
int[] terms;
for(int runs = 0; runs < 400; runs++)
{
terms[] = value;
}
For those who have used PHP, here's what I'm trying to do in C#:
$arr = array();
for ($i = 0; $i < 10; $i++) {
$arr[] = $i;
}
Thanks,
Ross
I am trying to get a random array like this
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
it shows 2 numbers randomly if I have $rand_keys = array_rand($input, 2);$rand_keys = array_rand($input, 5); but since I want all 5 to show it doesnt work. whats causing that. I need to use array_rand. thanks