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

Hello Monks,

I need a perl utility, which will read two files (user will provide two files), put the contents of each file into two separate arrays and by using some algorithm it will print the custom merging array results.

Please refer the two files

File_1 => cell_list

c_1_mm c_2_mm c_3_mm c_4_mm

File_2 => pvt_list

1 2 3 4 5 6 7 8 9 0

After running the utility it should print in a file according to below OUTPUT file.

c_1_mm_1 c_2_mm_1 c_3_mm_1 c_4_mm_1 c_1_mm_2 c_2_mm_2 c_3_mm_2 c_4_mm_2 c_1_mm_3 c_2_mm_3 c_3_mm_3 c_4_mm_3 c_1_mm_4 c_2_mm_4 c_3_mm_4 c_4_mm_4 c_1_mm_5 c_2_mm_5 c_3_mm_5 c_4_mm_5 c_1_mm_6 c_2_mm_6 c_3_mm_6 c_4_mm_6 c_1_mm_7 c_2_mm_7 c_3_mm_7 c_4_mm_7 c_1_mm_8 c_2_mm_8 c_3_mm_8 c_4_mm_8 c_1_mm_9 c_2_mm_9 c_3_mm_9 c_4_mm_9 c_1_mm_0 c_2_mm_0 c_3_mm_0 c_4_mm_0

I only written the few lines

#!/usr/bin/perl use strict; use warnings; my $cell_list = $ARGV[0]; # input cell_list my $pvt_list = $ARGV[1]; # input pvt_list if ($#ARGV!=1) { print "USAGE :: perl max_tran_update.pl <<cell_list>> <<PVT_LIST>> +\n\n" ; exit(1); } open (CFILE,"<","$cell_list") || die "Can not open cell_list"; open (PFILE,"<","$pvt_list") || die "Can not open pvt_list"; my @cell_file = <CFILE>; my @pvt_file = <PFILE>;

And trying to understand how I put those arrays in loop. Please help me to understand how this can be done.....

Replies are listed 'Best First'.
Re: Merging of custom array
by choroba (Cardinal) on Jul 19, 2019 at 12:09 UTC
    You need to loops. The inner loop will loop over the first array, the outer loop over the second one:

    for my $number (@pvt_file) { say join ' ', map $_ . "_$number", @cell_file; }

    (The inner loop is disguised as map).

    BTW, you need to chomp the arrays to remove the newlines.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Hello choroba, thanks for showing me the LIGHT form the Labyrinth. Yes I followed your instructions and got the desired results.....

      Thanks and best Regards,

      Anirban

Re: Merging of custom array
by Fletch (Bishop) on Jul 19, 2019 at 12:56 UTC

    For, schmor. MAP ALL THE THINGS!

    use 5.018; my @a = map {qq{c_${_}_mm}} 1 .. 4; my @b = ( 1 .. 9, 0 ); my $out = join( qq{\n}, map { join( q{ }, @{$_} ) } map { my $i = $_; [ map {qq{${_}_$i}} @a ] } @b ); say $out; __END__ c_1_mm_1 c_2_mm_1 c_3_mm_1 c_4_mm_1 c_1_mm_2 c_2_mm_2 c_3_mm_2 c_4_mm_2 c_1_mm_3 c_2_mm_3 c_3_mm_3 c_4_mm_3 c_1_mm_4 c_2_mm_4 c_3_mm_4 c_4_mm_4 c_1_mm_5 c_2_mm_5 c_3_mm_5 c_4_mm_5 c_1_mm_6 c_2_mm_6 c_3_mm_6 c_4_mm_6 c_1_mm_7 c_2_mm_7 c_3_mm_7 c_4_mm_7 c_1_mm_8 c_2_mm_8 c_3_mm_8 c_4_mm_8 c_1_mm_9 c_2_mm_9 c_3_mm_9 c_4_mm_9 c_1_mm_0 c_2_mm_0 c_3_mm_0 c_4_mm_0

    (Alternately: Yo dawg we heard you like maps . . .)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Merging of custom array
by BillKSmith (Monsignor) on Jul 19, 2019 at 14:03 UTC
    Here is another solution using map for the inner loop. Note the use of 'say'. The newline would have been a problem with print.
    >type anirbanphys2.pl use strict; use warnings; use feature 'say'; my @cell_file = qw( c_1_mm c_2_mm c_3_mm c_4_mm ); my @pvt_file = qw( 1 2 3 4 5 6 7 8 9 0 ); foreach my $pvt (@pvt_file) { say map { "${_}_$pvt "} @cell_file; } >perl anirbanphys2.pl c_1_mm_1 c_2_mm_1 c_3_mm_1 c_4_mm_1 c_1_mm_2 c_2_mm_2 c_3_mm_2 c_4_mm_2 c_1_mm_3 c_2_mm_3 c_3_mm_3 c_4_mm_3 c_1_mm_4 c_2_mm_4 c_3_mm_4 c_4_mm_4 c_1_mm_5 c_2_mm_5 c_3_mm_5 c_4_mm_5 c_1_mm_6 c_2_mm_6 c_3_mm_6 c_4_mm_6 c_1_mm_7 c_2_mm_7 c_3_mm_7 c_4_mm_7 c_1_mm_8 c_2_mm_8 c_3_mm_8 c_4_mm_8 c_1_mm_9 c_2_mm_9 c_3_mm_9 c_4_mm_9 c_1_mm_0 c_2_mm_0 c_3_mm_0 c_4_mm_0
    Bill
      The newline would have been a problem with print.

      Not if you set the OUTPUT_RECORD_SEPARATOR like say() does:

      local $\ = "\n";
        set the OUTPUT_RECORD_SEPARATOR like say() does

        I was surprised by this, and then noticed that you were referring to the say docs. However, IMHO they're wrong, say doesn't seem to set a local $\, so I just filed bug #134298 to hopefully clarify/fix the documentation.

        Update: A patch has been applied, the say docs in the next release will say:

        Just like print, but implicitly appends a newline at the end of the LIST instead of any value $\ might have.
Re: Merging of custom array
by jwkrahn (Abbot) on Jul 19, 2019 at 19:52 UTC
    #!/usr/bin/perl use strict; use warnings; @ARGV == 2 or die "USAGE :: perl max_tran_update.pl <<cell_list>> <<PV +T_LIST>> \n\n"; my $cell_list = $ARGV[0]; # input cell_list my $pvt_list = $ARGV[1]; # input pvt_list open CFILE, '<', $cell_list or die "Can not open cell_list because: $! +"; open PFILE, '<', $pvt_list or die "Can not open pvt_list because: $!" +; my @cell_file = map s/\s+\z/_X/r, <CFILE>; while ( <PFILE> ) { chomp; print join( ' ', map s/_\k.+\z/$_/r, @cell_file ), "\n"; }
      Delight to see your solution jwkrahn...

      Thanks,

      Anirban