-3

Per criticism, I have focused the question to PHP, but alternative solutions--including javascript, python, and Excel--are acceptable.

Given the following list how do break it into a tab-delineated csv at every nth record (i.e., 3) ?

Apples
Sweet and juicy, these babies are in season!
$2
Bananas
Packed with essential vitamins, our organic bananas are perfect for a snack!
$1
Peaches
Juicy Georgia peaches are available!
$2
Tomatoes
Our variety is the perfect mix of juicy and hearty for your summer salads!
$1
Cucumbers
These cucumbers pair with tomatoes for amazing salads.
$2

The desired output would be

Apples\tSweet and juicy, these babies are in season!\t$2\n
Bananas\tPacked with essential vitamins, our organic bananas are perfect for a snack!\y$1\n
Peaches\tJuicy Georgia peaches are available!\t$2\n
Tomatoes\tOur variety is the perfect mix of juicy and hearty for your summer salads!\t$1\n
Cucumbers\tThese cucumbers pair with tomatoes for amazing salads.\t$2\n

I have attempted this exercise in SublimeText with regex but cannot master the pattern to match every nth occurrence.

Any solution will do. I just need to parse thousands of records into a spreadsheet.

mike
  • 1,680
  • 2
  • 19
  • 31
  • I didn't downvote, but the answer here seemingly seems trivial. You would also want to include your expected output and what you've tried so far. I see obvious alternatives to regex as well, so you may do well in specifying a programming language. – code Jun 02 '22 at 23:24
  • I left this open INTENTIONALLY to allow for a range of solutions. As I stated, any solution will do. This community used to be open to tackling problems this way. – mike Jun 03 '22 at 13:41
  • @code this is a practical problem ripe for demonstrating how to arrive at solutions. Please revisit and propose your solution. – mike Jun 03 '22 at 13:53
  • 1
    Now that you added the desired output it's a lot more clear what you meant. I wouldn't tackle this problem with regex only, which is why I asked for the language. I don't have a lot of experience in PHP, but for JavaScript you might do `input.split("\n").reduce((prev, cur, i) => (!(i % 3)) ? prev.concat([[cur]]) : (prev[(i / 3) | 0].push(cur), prev), []).map(i => i.join("\t"))` (returns array). – code Jun 03 '22 at 18:18
  • 1
    Actually in PHP you could also match nth occurrences: https://stackoverflow.com/questions/28438693/regex-match-nth-occurrence – code Jun 03 '22 at 18:18
  • @code thank you for the suggestions. the php link matches everything up to the nth character. i need to match every nth character. trying to interpret your javascript. – mike Jun 04 '22 at 02:02
  • The concept in the JS is simple: split the string into an array with new lines as a delimiter, then create a new array with every 3 items merged into 1. As to practice, the (even short) code got slightly complicated. Shouldn't be too hard to convert to PHP especially with a for loop incrementing by 3 though. – code Jun 04 '22 at 02:10

0 Answers0