I have the following input data, for which I'd like to remove repeated elements (leaving all strings in the same order of appearence) within each group and each sub group. A group begins with a string that has related s5
, in this case
all below "FIRST CHAPTER" and the next group begins in first appearence of "SECOND CHAPTER". Within each group could be sub groups that are related with s4
. For example "FIRST PART",
"INTRODUCTION", "SECOND PART", etc.
The input is like column on the left. The second column is the explanation that shows the occurrences of each string within the group and within group/sub group. The 3rd column is the expected output and the 4th column is the output I'm getting currently.
I've highlighted in yellow the first appearence of each string to show you better which elements should be printed in output. Those in yellow are the first appearence in their respective group/subgroup and removing all lines in white, we get the correct output. I hope make sense.
This is my current code, where the logic looks the uniq values. The output is similar but not correct, since the uniq values are compared agains the whole array and not agains each group.
a=<<_
s5>>FIRST CHAPTER
s4>>FIRST PART
s4>>INTRODUCTION
s3>>Article 1
s5>>FIRST CHAPTER
s4>>FIRST PART
s4>>INTRODUCTION
s3>>Article 2
s5>>FIRST CHAPTER
s4>>SECOND PART
s4>>REVIEW
s3>>Article 1
s5>>FIRST CHAPTER
s4>>SECOND PART
s4>>METHODOLOGY
s3>>Article1
s5>>SECOND CHAPTER
s4>>FIRST PART
s4>>INTRODUCTION
s3>>First section
s5>>SECOND CHAPTER
s4>>FIRST PART
s4>>INTRODUCTION
s3>>Second Section
_
b = a.split("\n")
c = b.uniq
puts c
May someone help me in how to do this. Thanks
Input and Output below
| Input | Output |
|---------------------- |-------------------- |
| s5>>FIRST CHAPTER | s5>>FIRST CHAPTER |
| s4>>FIRST PART | s4>>FIRST PART |
| s4>>INTRODUCTION | s4>>INTRODUCTION |
| s3>>Arcticle 1 | s3>>Arcticle 1 |
| s5>>FIRST CHAPTER | s3>>Arcticle 2 |
| s4>>FIRST PART | s4>>SECOND PART |
| s4>>INTRODUCTION | s4>>REVIEW |
| s3>>Arcticle 2 | s3>>Arcticle 1 |
| s5>>FIRST CHAPTER | s4>>METHODOLOGY |
| s4>>SECOND PART | s3>>Arcticle1 |
| s4>>REVIEW | s5>>SECOND CHAPTER |
| s3>>Arcticle 1 | s4>>FIRST PART |
| s5>>FIRST CHAPTER | s4>>INTRODUCTION |
| s4>>SECOND PART | s3>>First section |
| s4>>METHODOLOGY | s3>>Second Section |
| s3>>Arcticle1 | |
| s5>>SECOND CHAPTER | |
| s4>>FIRST PART | |
| s4>>INTRODUCTION | |
| s3>>First section | |
| s5>>SECOND CHAPTER | |
| s4>>FIRST PART | |
| s4>>INTRODUCTION | |
| s3>>Second Section | |