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

I have an array containing 0001.tif, 0002.tif, 0003.tif, 0004.tif, 0005.tif upto 1245.tif and I want to build subsets: subset a: 0001.tif, 0005.tif, 0009.tif ... etc subset b: 0002.tif, 0006.tif, 0010.tif ... etc
#!/usr/bin/perl # sort_subsets.pl use warnings; use strict; # full array contains 1245 tif files, example contains 10 my @fullarray = qw(0001.tif 0002.tif 0003.tif 0004.tif 0005.tif 0006.t +if 0007.tif 0008.tif 0009.tif 0010.tif) my @subsetA () my @subsetB ()
I don't know exactly what to put in the my @subsetA brackets. thanks, appreciate the help.

Replies are listed 'Best First'.
Re: arrays help
by Ratazong (Monsignor) on Oct 27, 2010 at 08:24 UTC

    Seems that you want to fill @subsetA and @subsetB based on the filenames contained in @fullarray.

    I would propose the following apporach

    • loop though each element of @fullarray
    • use a regex to extract the relevant part of the filename (e.g. /(\d\d\d\d)\.tif)
    • analyze the relevant part (e.g. use the modulo-operator (%) to get the reminder if of the number divided by 4)
    • based on the result of the analysis, push the filename to one of your subsets
    HTH, Rata
Re: arrays help
by mjscott2702 (Pilgrim) on Oct 27, 2010 at 08:33 UTC
    Not quite sure how you are separating them - "odd" files into one array, "even" ones into the other?

    If so, do as Ratazong described, but use 2 rather than 4 in your modulo (%) statement.

Re: arrays help
by kcott (Archbishop) on Oct 27, 2010 at 09:27 UTC

    Here's another way of doing it:

    #!perl use 5.12.0; use warnings; my @full = qw{ 0001.tif 0002.tif 0003.tif 0004.tif 0005.tif 0006.tif 0007.tif 0008.tif 0009.tif 0010.tif 0011.tif 0012.tif 0013.tif 0014.tif 0015.tif 0016.tif 0017.tif 0018.tif 0019.tif 0020.tif }; my @partioned = ([],[],[],[]); for (0 .. $#full) { state $i = 0; push @{$partioned[$i]}, $full[$_]; $i = ++$i % 4; } my @subA = @{$partioned[0]}; my @subB = @{$partioned[1]}; my @subC = @{$partioned[2]}; my @subD = @{$partioned[3]}; say qq{@subA}; say qq{@subB}; say qq{@subC}; say qq{@subD};

    Which outputs:

    $ tif_split.pl 0001.tif 0005.tif 0009.tif 0013.tif 0017.tif 0002.tif 0006.tif 0010.tif 0014.tif 0018.tif 0003.tif 0007.tif 0011.tif 0015.tif 0019.tif 0004.tif 0008.tif 0012.tif 0016.tif 0020.tif

    -- Ken

Re: arrays help
by Khen1950fx (Canon) on Oct 27, 2010 at 10:05 UTC
    I tried this. It'll do what you want:
    #!/usr/bin/perl use strict; my @array = ( '0001.tif ', "\n", '0002.tif ', "\n", '0003.tif ', "\n", '0004.tif ', "\n", '0005.tif ', "\n", '0006.tif ', "\n", '0007.tif ', "\n", '0008.tif ', "\n", '0009.tif ', "\n", '0010.tif ', "\n"); my @even = grep ($_ % 4 == 1, @array); print "@even\n"; my @odd = grep ($_ % 4 - 1 == 1, @array); print "@odd\n";
      Your solution is great, but i want to correct it.

      it must be look like

      my @subA = grep( $_ % 4 == 1, 0..$#array ) my @subB = grep( $_ % 4 == 2, 0..$#array ) e.t.c.

Re: arrays help
by Khen1950fx (Canon) on Oct 27, 2010 at 22:15 UTC
    Using Data::PowerSet, I took it up a notch. This will give you subsetA and subsetB plus all the subsets of the subsets:
    #!/usr/bin/perl use strict; use Data::PowerSet qw(powerset); my @array = ( '0001.tif ', "\n", '0002.tif ', "\n", '0003.tif ', "\n", '0004.tif ', "\n", '0005.tif ', "\n", '0006.tif ', "\n", '0007.tif ', "\n", '0008.tif ', "\n", '0009.tif ', "\n", '0010.tif ', "\n"); my $powersetA = powerset( grep ($_ % 4 == 1, @array)); for my $p(@$powersetA) { print @$p, "\n"; } my $powersetB = powerset( grep ($_ % 4 == 2, @array)); for my $p(@$powersetB) { print @$p, "\n"; }