corfuitl has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I have two arrays (arr1 and arr2) and I would like to split them into chunks based on a third, 2d array.

The 2d array contains the values below, which correspond to arr positions. For instance,

Arr1 Arr2 3 4 5 5 6 6 8 10

I want to create files that contain the arr1 and arr2 contents. For instance:

File-arr1-1.txt arr1[0], arr1[1], arr1[2] File-arr2-1.txt arr2[0], arr2[1], arr2[2], arr2[3] File-arr1-2.txt arr1[3] File-arr2-2.txt arr2[4] File-arr1-3.txt arr1[4] File-arr2-3.txt --- File-arr1-4.txt arr1[5] File-arr2-4.txt arr2[5] File-arr1-5.txt arr1[6] File-arr2-5.txt arr2[6] File-arr1-6.txt arr1[7] File-arr2-6.txt arr2[7], arr2[8], arr2[9] File-arr1-7.txt arr1[8] File-arr2-8.txt arr2[10]

So... all in all... I want in separate files the difference and in separate files the contents of the 2d array.

Here is a piece of my code but it doesn't work

my $start = 0; my $end = 0; for my $i (0 .. $#al) { $end = $al[$i][0]; for my $s ($start .. $end-1){ print "$start\t$end\n"; if ($s == $end-1){ $start = $end+1; } } }

Replies are listed 'Best First'.
Re: splitting 2D array into chunks
by shmem (Chancellor) on Jun 21, 2021 at 18:46 UTC

    Your question is poorly formulated. Try again. It looks like you want the gaps too, in separate files.

    Also, your description of the desired output is inconsistent. Why is

    File-arr2-3.txt ---

    if you don't have an empty slot in the 2d array?

    Also, file File-arr1-8.txt seems to be missing - if both arrays are aligned. Then it should contain

    arr1[9],arr1[10]

    Hint: to write to files, you have to open them, then print to them, close them.
    Can't say more about that.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: splitting 2D array into chunks
by BillKSmith (Monsignor) on Jun 21, 2021 at 20:21 UTC
    I really do not understand your requirements. My best guess is you want to partition the array @arr1 according to the specification in the first column of array @a1. (and @arr2 according to the second column.) The 'end' indicies in @arr2 follow the math convention of 'start-at-one' rather than Perl's 'start-at-zero'. You are having a problem with the partitioning, not writing to files. The following code only does @arr1 and does not produce exactly what you expect. I hope you can use it as an example when you repost your question.
    use strict; use warnings; my @arr1 = 'a' .. 'j'; my @al = ( # Arr1 Arr2 [ 3, 4 ], [ 5, 5 ], [ 6, 6 ], [ 8, 10 ], ); my $start = 0; my $end = 0; for my $i (0 .. $#al) { $end = $al[$i][0]-1; print @arr1[$start .. $end], "\n"; $start = $end+1; }

    OUTPUT:

    abc de f gh
    Bill
Re: splitting 2D array into chunks
by AnomalousMonk (Archbishop) on Jun 21, 2021 at 20:34 UTC

    I agree your question is badly posed; I, at least, don't really understand the details.

    See How to ask better questions using Test::More and sample data. You might start with something like

    use strict; use warnings; use Test::More 'tests' => <number of tests>; use Test::NoWarnings; # adds 1 test use Data::Dumper; # for debug my @al = (...); my @distribution = ( 3, 4, 5, 5, 6, 6, 8, 10, ); my $ar_expected_output = ( [ ... ], [ ... ], ... ); my $ar_got_output = distribute(\@al, \@distribution); is_deeply $ar_got_output, $ar_expected_output; exit; # subroutines ############################################# sub distribute { my ($ar_input, # array ref.: input array $ar_distribution, # array ref.: distribution to output arrays ) = @_; my @output; ... return \@output; }
    Construct the output arrays first and test the process. Once you have the output arrays constructed, it should be fairly straightforward to output them to files.

    See also Short, Self-Contained, Correct Example and Perl Data Structures Cookbook.

    See perlintro for basic file I/O stuff, open and perlopentut for more advanced stuff.

    Maybe also see I know what I mean. Why don't you?


    Give a man a fish:  <%-{-{-{-<