-1

I have the following string A B C D E F G H I J. I want to make an array out of it, so I use this

my @string = split/(\W)/, $text;

It works ok, and I get an array of 20 lines looking like this:

A

B

C

D

...

Now, I want to split this array in a given number of pieces (let's say 4 in this case), thus resulting I imagine in 4 smaller arrays of 5 lines each, looking like this @1 = A, ,B, ,C , @2 = ,D, ,E,, @3 = F, ,G, ,H and @4 = ,I, ,J,. Finally, I want to interleave these 4 arrays in order to get an output array in which I first have the first line of each array, the the second line of each array, and so on... the final thing looking like this :

A

F


D

I
B

G


E

J
C

H

How to achieve this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
chris202
  • 191
  • 1
  • 9
  • Your `split` will add the whitespace intervals as separate elements. But you seem to know this, as you say `@1 = A, ,B, ,C` etc. It is very hard to believe that this is what you want, so please confirm. – Borodin Feb 03 '15 at 00:05
  • 2
    Are you sure that what you need isn't a Perl tutorial? You seem to be stumbling over very basic concepts. – Borodin Feb 03 '15 at 00:06
  • 1
    You have been directed towards the module `List::MoreUtils`, with its functions `zip/mesh` in your previous, identical question. That question was marked as a duplicate, because it was already answered. Posting the same question again will not accomplish anything. If you're having problems applying the suggested solution to your problem, ask a question about the code you have tried. That is what this site is for. Not for getting free code. – TLP Feb 03 '15 at 00:06
  • The linked question was http://stackoverflow.com/questions/38345/is-there-an-elegant-zip-to-interleave-two-lists-in-perl-5 – TLP Feb 03 '15 at 00:07
  • @TLP: If you are saying that this is a duplicate question then please mark it as such. Malevolent comments aren't useful. – Borodin Feb 03 '15 at 00:11
  • @Borodin I already closed the previous question he posted, which was identical to this one. Apparently he needed more information, which is what I gave him. If you think that is malevolent, that is only in your own mind, and I encourage you to read my comment with an open mind. If I "mark" this question as a duplicate, it is immediately closed -- because of my previleges -- and I thought that it would be fair if I first gave him a chance to fix it. – TLP Feb 03 '15 at 00:24
  • 1
    You mentioned in a comment on your previous question that you tried the first solution in the linked duplicate, but it wasn't working for you. Please [edit] your question to include the code you tried, the output you got, and any errors. Or, as @TLP has mentioned several times, simply use the `zip` function from [`List::MoreUtils`](https://metacpan.org/pod/List::MoreUtils)...that would probably be easier. – ThisSuitIsBlackNot Feb 03 '15 at 00:28
  • @ Borodin: yes I confirm. Maybe I need a tutorial in fact, I'm a beginner so... @ TLP: Yes I have ask a new question because I was asked to do so by ThisSuitIsBlackNot in a comment on my previous question, and I was told to expose the exact code I was using, so here is the reason why I've posted this new question. – chris202 Feb 03 '15 at 09:11
  • Ok I've used the `zip` function (which I didn't know of before) and it works perfect. Thank you all ! – chris202 Feb 03 '15 at 09:39
  • @chris202 Glad it worked for you. I'm sorry that the duplicate we linked to your first question wasn't helpful...unfortunately, the best and simplest answer (`zip` or `merge` from `List::MoreUtils`) is not the highest-voted answer to that question, and there was no example of how to use `zip` in the answer so I understand how you could have missed it (I've added a usage example to make it clearer). In the future, if your question is closed as a dupe, you may need to try several of the answers, not just the top one. – ThisSuitIsBlackNot Feb 03 '15 at 16:03
  • @chris202 As for tutorials: there are a lot of really old Perl tutorials floating around on the web that demonstrate horrible programming practices. I'd recommend one of [these modern Perl tutorials](http://perl-tutorial.org/#index3h1). Personally, I started with "Learn Perl in about 2 hours 30 minutes," but there are many good ones out there. Also, learn to love Perl's built-in documentation, `perldoc`. If installed on your system, you can run `perldoc perlvar` to learn about built-in variables, `perldoc perlop` to learn about operators, `perldoc -f ` (e.g. `perldoc -f sort`) – ThisSuitIsBlackNot Feb 03 '15 at 16:06
  • *(continued)* to learn about built-in functions, `perldoc Module::Name` (e.g. `perldoc List::MoreUtils`) to learn about installed modules, and much more. The documentation is fantastic and should be your #1 go-to resource for Perl questions (yes, even before Stack Overflow). Good luck! – ThisSuitIsBlackNot Feb 03 '15 at 16:08
  • @ThisSuitIsBlackNot Thanks for all the good info, I'll definitely have a look at it ! – chris202 Feb 03 '15 at 21:44

1 Answers1

0
  1. To make a slice of an array in Perl:

    my @oldArray = (a, ,b, ,c, ,d, ,e, ,f, ,g, h, ,i, ,j, );
    
    my @newArray1 = @oldArray[elemIndex1,elemIndex2,elemIndexN];
    

    OR

    my @newArray1 = @oldArray[firstElemIndex..lastElemIndex]
    

    Where elemIndex is the index of the element you want in the newArray array.

    firstElemIndex..lastElemIndex is used when you want consecutive indexes to be in the sliced array.

  2. To add elements at the end of an array:

    push(@interleavedArray,$newArray1[elemIndex]);
    

    Where $newArray1[elemIndex] is the element from @newArray1 you want to add to @interleavedArray

  3. To add elements in order:

    Just do a for loop:

    for my $index (0..3){
        push(@interleavedArray,$newArray1[$index]);
        push(@interleavedArray,$newArray2[$index]);
        push(@interleavedArray,$newArray3[$index]);
        push(@interleavedArray,$newArray4[$index]);
        }
    
kidkamek
  • 426
  • 4
  • 7