-2

So I have a set of variables in an array, and I would like to collect the data from every of (let's say) 4th index. For example if I have array all[] which has data at [0]->id [1]->field1 [2]->field2 and after a [3]->linebreak and after another set of [4]->id [5]->field1 and [6]->field2 [7]->linebreak and so on and so forth. Now I'd like to create an array(or a list) called id[] which would have all the id's in it and a field1[] which would have all the field ones. How could I do this?

Douglas
  • 53,759
  • 13
  • 140
  • 188
Greg Zetko
  • 93
  • 2
  • 8
  • 5
    `How could I do this?` is not a question. It implies *"write it for me"*. Show your effort and what you have done so far. – EZI Aug 21 '15 at 19:56
  • 2
    Seems like a bad design decision to put multiple kinds of things in the same array. – Joel Coehoorn Aug 21 '15 at 20:00
  • I'm just interested in an algorithm. I was thinking of creating multiple for loops which would start at all[0] and maybe load every 4th element by but it would need me to write a lot of for algorithm which I'd not like and it looks bad and it is not a good idea in general. I'm sure someone out there has a better way to solve it. – Greg Zetko Aug 21 '15 at 20:00
  • 1
    Reading again: looks like an array of strings from a file. I get where that comes from, but there are still better (_much better_) ways to read the file. – Joel Coehoorn Aug 21 '15 at 20:01
  • I have to agree with @EZI on this one. Best way to learn is to try. Our brain has the tendency to learn best when we get things wrong, because once we get it right, that knowledge is stuck with us for a very long time. Of course, if you're completely and hopelessly stuck with the code you create, post it on here and a push in the right direction should set things straight. – B.K. Aug 21 '15 at 20:02

2 Answers2

5

You can apply modulo 4 on the index of each array element to determine its position along its group-of-four. Ids would be in position 0; fields would be in position 1 and 2.

int[] ids = all.Where((_, i) => i % 4 == 0).ToArray();
int[] fields = all.Where((_, i) => i % 4 == 1 || i % 4 == 2).ToArray();
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • I'd never considered to use `_` by itself as a variable name. cute. – Joel Coehoorn Aug 21 '15 at 20:02
  • Thank you this is what I was looking for – Greg Zetko Aug 21 '15 at 20:02
  • 1
    @JoelCoehoorn: I like the `_` here too. I once used the unicode snowman as a variable in a javascript function. Haven't tried that in C# yet. – Sam Axe Aug 21 '15 at 20:04
  • Now it looks Haskell-ish. – Kapol Aug 21 '15 at 20:09
  • 3
    @JoelCoehoorn: It's a naming convention to indicate that the parameter will not be used in the anonymous function. It originates from functional languages (such as Haskell), but it's [picking up traction](http://stackoverflow.com/a/10538946/1149773) in C# lambdas too. – Douglas Aug 21 '15 at 20:11
  • Okay, so this was the solution I was looking for but I realized that it would be better if I just redesign my algorithm and have different types of variables in different arrays from the start as @JoelCoehoorn said – Greg Zetko Aug 21 '15 at 20:33
1

You might find it easier to store the data in an object...

public class DataObject {

    public DataObject() {}
    public DataObject(string[] fields) {   
        // this is an example.  construct a .ctor that is sane for your data.
        // TODO:  Add array bounds checking and data sanity checks
        this.ID = fields[0];
        this.Field1 = fields[1];
        this.Field2 = fields[2];
    }

    public string ID {get; set;}

    public string Field1 {get; set;}

    public string Field2 {get; set;}
}

Then you would store an array or array-like structure of DataObject.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89