Hello everyone, happy Friday.

Please optimize my code or give suggestion on how to do it. On a big array (35k elements), it take over 1.5 minute to compute. Any suggestion is greatly appreciated. I have included a section of my code and an example of the array all_listing (first line). Thank you.

@all_listing = qw ('folder/file.000001.jpg','folder/file.000002.jpg', +... , 'folder/file.039000.jpg','folder/file.040000.jpg'); foreach $kk (@all_listing) { next if $kk eq ''; # next on empty line next if ($kk =~ m|(.+)\/\..+| and $hidden); # omit hidden (do +t) files if ($kk =~ m|(.+)\/(\d+)\.(.+$)|) { # match image sequence wi +thout prefix $path = $1; $num = $2; $ext = $3; print "$path: $num.$ext\n" if ($debug); $pathpre = "$path/$ext"; # path and ext as the key ; ad +ding ext so that if there are 2 seq with the same prefix and diff ext +ension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } elsif ($kk =~ m|(.+)\/(.+?)([\._]+)(\d+)\.(.+$)|) { # match + image sequence $path = $1; $pre = $2; $div = $3 ; $num = $4; $ext = $5; print "$path: $pre$div$num.$ext\n" if ($debug); $pathpre = "$path/$pre$div$ext"; # path, prefix, divide +r and ext as the key ; adding ext so that if there are 2 seq with the + same prefix and diff extension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } elsif ($kk =~ m|(.+)\/(.+?)(\d+)\.(.+$)|) { # match most im +age sequence, except this 7R01.0118762.dpx (number before dot) ; abov +e regex takes care of this match $path = $1; $pre = $2; $num = $3; $ext = $4; print "$path: $pre$num.$ext\n" if ($debug); $pathpre = "$path/$pre$ext"; # path, prefix and ext as +the key ; adding ext so that if there are 2 seq with the same prefix +and diff extension print ">> $pathpre\n" if ($debug); if (exists $seq{$pathpre}) { push (@range_tmp,$num); # add element to array } else { $seq{$pathpre} = $num; # create hash @range_tmp = $num; # empty array and add new elem +ent to it } $new_seq{$pathpre} = [@range_tmp]; # create new seq hash $ext{$pathpre} = $ext; # create hash of extension } else { push (@no_match,$kk); } }

In reply to Optimize my foreach loop / code by rmocster

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.