www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Adjacent item in a array

reply Vino <vino.bheeman hotmail.com> writes:
Hi All,

  Request you help on printing an array in below,

Eg:

Array ("T1", "T2", "T3", "T4", "T5")

Output required as below

T1,T2
T2,T3
T3,T4
T4,T5


From,
Vino.B
Feb 03
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 3 February 2018 at 19:13:05 UTC, Vino wrote:
  Request you help on printing an array in below,
Try looping through the array printing the current item followed by the item after the current item. Foreach with index may be helpful. Consider what happens on the last element.
Feb 03
parent Vino <vino.bheeman hotmail.com> writes:
On Saturday, 3 February 2018 at 19:19:00 UTC, Adam D. Ruppe wrote:
 On Saturday, 3 February 2018 at 19:13:05 UTC, Vino wrote:
  Request you help on printing an array in below,
Try looping through the array printing the current item followed by the item after the current item. Foreach with index may be helpful. Consider what happens on the last element.
Hi Ruppe, I have tired it , it prints as expected but getting range violation when it reaches the last element foreach(j, k; D1[].enumerate(1)) writeln(k,D1[j]); From, Vino.B
Feb 03
prev sibling parent reply Seb <seb wilzba.ch> writes:
On Saturday, 3 February 2018 at 19:13:05 UTC, Vino wrote:
 Hi All,

  Request you help on printing an array in below,

 Eg:

 Array ("T1", "T2", "T3", "T4", "T5")

 Output required as below

 T1,T2
 T2,T3
 T3,T4
 T4,T5


 From,
 Vino.B
2.079 [1, 2] will ship with slide: --- auto arr = ["T1", "T2", "T3", "T4", "T5"]; arr.slide(2).each!writeln; --- https://run.dlang.io/is/YZsQCf slide is super-powered with a lot of nice things to generate sliding windows lazily, but for this simple case, it's enough to do: --- auto arr = ["T1", "T2", "T3", "T4", "T5"]; arr.zip(arr.dropOne).each!(a => writefln("%s,%s", a[0], a[1])); --- https://run.dlang.io/is/R6IyYV or of course, as Adam pointed out, good 'ld array iteration works too: --- auto arr = ["T1", "T2", "T3", "T4", "T5"]; foreach (i; 0 .. arr.length - 1) writefln("%s,%s", arr[i], arr[i + 1]); --- https://run.dlang.io/is/fb3JYI [1] https://dlang.org/changelog/pending.html#std-range-slide [2] https://dlang.org/phobos-prerelease/std_range.html#slide
Feb 03
parent reply Vino <vino.bheeman hotmail.com> writes:
On Saturday, 3 February 2018 at 19:28:01 UTC, Seb wrote:
 On Saturday, 3 February 2018 at 19:13:05 UTC, Vino wrote:
 [...]
2.079 [1, 2] will ship with slide: --- auto arr = ["T1", "T2", "T3", "T4", "T5"]; arr.slide(2).each!writeln; --- [...]
Hi Seb; Thank you very much. From, Vino.B
Feb 03
parent Vino <vino.bheeman hotmail.com> writes:
On Saturday, 3 February 2018 at 22:58:04 UTC, Vino wrote:
 On Saturday, 3 February 2018 at 19:28:01 UTC, Seb wrote:
 On Saturday, 3 February 2018 at 19:13:05 UTC, Vino wrote:
 [...]
2.079 [1, 2] will ship with slide: --- auto arr = ["T1", "T2", "T3", "T4", "T5"]; arr.slide(2).each!writeln; --- [...]
Hi Seb; Thank you very much. From, Vino.B
Hi Seb, Was going through the change log of version v2.079, and it has most of the function that I require, so hope the release date of v2.079 would be 1st/March, correct me if i am wrong. From, Vino.B
Feb 11