Use splice, although, some might not recommend it. I use it extensively, when working with arrays, since, it greatly simplifies and shortens your code. Also, it's very efficient.

Your code:
for my $key (keys %coll_key_hash) { foreach my $rec (@recs_read) { my @fields = split(/\|/, $rec); if ($fields[0] eq '010') { if ($fields[3] =~ $key) { $print_record = 1; if ($key ne $prev_key) { $prev_key = $key; print_record ("CUST-BEG|$seq\n",' '); } print_record ("$rec\n",'c'); } else { $print_record = 0; } } else { if ($print_record) { print_record ("$rec\n",'c'); } } } }
First, I would like to simplify it, in order to work with it easier:
for my $key (keys %coll_key_hash){ for my $rec (@recs_read){ my @fields = split(/\|/, $rec); if($fields[0] eq '010'){ $print_record = 0; if($fields[3] =~ $key){ #shouldn't this be /$key/ ? $print_record = 1; unless($key eq $prev_key){ $prev_key = $key; print_record ("CUST-BEG|$seq\n",' '); } } } if($print_record){ print_record ("$rec\n",'c'); } } }
The splice algorhythm to delete an array record and to check for loop iteration consistence:
for my $key (keys %coll_key_hash){ for(my $recIdx = 0; $recIdx < scalar @recs_read; $recIdx++){ #check, if redo overflowed the end of an array: next if(($recIdx + 1) > scalar @rec_read); my $rec = $recs_read[$recIdx]; my @fields = split(/\|/, $rec); if($fields[0] eq '010'){ $print_record = 0; if($fields[3] =~ $key){ #shouldn't this be /$key/ ? $print_record = 1; unless($key eq $prev_key){ $prev_key = $key; print_record ("CUST-BEG|$seq\n",' '); } } } if($print_record){ print_record ("$rec\n",'c'); #Delete one record at the specified index: splice(@recs_read, $recIdx, 1); #Since the record at the current index is deleted, the new + record shifted to the same index, so we test it again: redo; } } }

In reply to Re: Deleting records from an array by igoryonya
in thread Deleting records from an array by viffer

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.