Dear Masters,
With code below I intend to concatenate the array ($aref1 and $aref2) sequentially and intercepted with certain values in between.
use Data::Dumper; $Data::Dumper::Indent = 0; my $aref1 = [ 'D 4', 'E 6' ]; my $aref2 = [ 'A 0', 'B 1', 'C 2', 'A 3', 'B 4' ]; my $slen = 5; concat($aref1,$slen); # This already give correct result concat($aref2,$slen); # This still incorrect sub concat { my ($aref,$slen)= @_; my @concat; foreach my $i ( 1 .. $#{$aref} ) { my @out = split (" ", $aref->[$i-1]); my @in = split (" ", $aref->[$i]); # value to be put in (intercepted) my $val = $in[1]-$out[1]-$slen; push @concat, ($out[0],$val,$in[0]); } print Dumper \@concat ; return ; }

Currently, only $aref2 still gives incorrect result as follows:
$VAR1 = ['A',-4,'B','B',-4,'C','C',-4,'A','A',-4,'B'];
As you can see, the result above still contain the 'redundant' elements like B-B, C-C, A-A.
How can I change my code above so that it gives the following correct expected results:
$VAR1 = ['D',-3,'E']; # from $aref1 $VAR1 = ['A',-4,'B',-4,'C',-4,'A',-4,'B']; # from $aref2


---
neversaint and everlastingly indebted.......

In reply to Concatenating Non-Redundant Elements into an Array by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.