Extract data from arrays

I

ilr

Hi

This is probably fairly simple but I am newish at programming and was
wondering if someone can give me some advice on handling the following.

I have an array with a large number of elements in it. 0-9 are related
data, 10-19, 20-29 are related and so on. What is the best way of extracting
groups of elements from the array into another array where each element is
the related data or to extract say elements 0,1,5 from the first group,
10,11,15 from the second etc.

I have tried iterating through the array with if then statements but for a
large array this gets messy.

Any advice would be greatly appreciated.

Regards
Ian
 
T

tomb

If the elements are always 10, or some such number, that are related,
why not use a multi dimensional array instead?

T
 
I

ilr

Thanks for replying tomb.

The array is returned from a 3rd pary library so I don't really have much
choice in that respect.
 
S

Steven Cheng[MSFT]

Hi Lan

Sounds like this is an Algorithm question :)

I'm still not very sure about the exact result you want.

My understanding is you have a source array which may contains a large
sequence of elements. And a certain sub sequence of elements in the array
are of the same group(according to their index in array or their value?)
and you want to extract them out from the array and make the elements of
the same group together, correct?

As Tomb mentioned the "two-dim" array approach, I also think this is a good
idea if the interval or size of each group are fixed. You can create a
two-dimensional array in your code and copy all the elements from original
array into the two dimensional array.

Also, I think it would be helpful you provide more info such as some
example data(what's the original array and what you want to make them be
tranformed into and how you will use them after transformed). Thus, we can
look for some further ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
I

ilr

Thanks Steven

To give you a bit more background I am reading data from a piece of
hardware. A 3rd party library returns a 1 dim array that includes history
readings from that piece of hardware. Every 10 elements is a "row" of
history data. At the moment I am writing the rows to a csv file using the
following code.

For i = 0 To 99
sw.Write(readvals(i) & ",")
If i.ToString.EndsWith("9") Then
sw.Write(vbCrLf)
End If
retries = 0
Next

It works but I don't know whether there is a better way of handling the 10th
row? Or perhaps another way of spliting the array into a multi dimensional
array that might give a little bit more control to access specific elements
in each row.

Here is a sample of the data from my csv file. The first number is an index
of sorts but could be any range of sequential numbers not necessarily 1,2,3

1,0,219,18,3510,3219,2314,798,126,36,
2,0,819,13,3492,3232,2312,1396,64,23,
3,0,1419,6,3501,3224,2315,1278,24,12,

Appreciate your assistance.
 
J

Jack Jackson

Your code puts a trailing comma on every line. I don't know if you
really want that, my code doesn't.

Dim indx As Integer = 0
Dim totalCount As Integer = 100
Dim numCol As Integer = 10

Do While indx < totalCount
For col As Integer = 0 To numCol - 1
If col > 0
sw.Write(",")
End If
sw.Write(readvals(indx))
indx += 1
Next
sw.Write(vbCrLf)
End Do

You could also replace the If / End If and the two sw.Write calls
with:

sw.Write(String.Format("{0}{1}", Iif(col > 0, ",", ""),
readvals(indx))
 
S

Steven Cheng[MSFT]

Thanks for your quick reply Lan,

well, for moving the elements from the original one-dim array to a new
store, I think one time loop/sweep through the array is necessary and your
code logic look ok.

However, if you want to randomly access a certain elemente in a given
group(based on its 0 based index) and the groups are one by one (10
elemens per group) in original array, you can use the following code to
calculate the group index and element index(in group)

i=0 to max element index (in one-dim array)

int row = i / 10;
int col = i %10;

reversely, if you're given row, col, you can also calcualte which index(i)
you should use to locate the element in the original one-dim array. Does
this help some for your element accessing?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Thread-Topic: Extract data from arrays
From: =?Utf-8?B?aWxy?= <[email protected]>
Subject: Re: Extract data from arrays
Date: Thu, 7 Feb 2008 22:01:01 -0800
 
C

Cor Ligthert[MVP]

ilr,

This is so easy in VB

\\\
For i = X To 99 Step 10
'What you want to do
Next
///

Cor
 
I

ilr

Thanks guys

That has been very helpful. Just what I needed, new ways of looking at this
stuff.

Appreciate it.
 
I

ilr

Thanks Cor

I had considered step. Count gives me each 10th value. I was looking to
group the 10 values together.

Regards
ilr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top