Can you do it? Yes. Should you do it? No. map is appropriate for situations where the changes to each element in an array happen independently of the next. Furthermore, map would essentially be making you do redundant work.

Using map for processing that requires look-ahead within the token array is stretching the presumed meaning of map and will make your code appear convoluted to future maintainers. Unless you plan on holding this job until your script is ready for the dust bins, I would advise against trying to cram your entire algorithm within map. I would recommend doing your processing in a foreach loop.

As you are discovering, there is no way to process addresses without an awareness of context. Whether you do it with map or not, you are still going to have to make significant changes to your code to (a) check to see if a token can be normalized without knowing what is next (b) keep a list of pending tokens that haven't yet been normalized (c) determine when you have enough context to normalize any pending tokens.

If you were to do this with map, then you also would need to modify map {...} to return a list of pending tokens that were normalized rather than a single scalar as it does now:

my @aDone = map { push @aPending, $_; my @aNormalized; while (scalar(@aPending)) { my $sPending = shift @aPending; my $sNormalized = normalize($sPending, [@aPending]); if (!defined($sNormalized)) { #not enough information to normalize yet #so put the token back into the pending list unshift @aPending, $sPending; last; } push @aNormalized, $sNormalized; } @aNormalized; #return all normalized tokens } @aUnNormalized;

If you look carefully at the above code, you will note that you are doing an extra copy. First you are pushing normalized elements into @aNormalized. Then you are appending @aNormalized to @aDone. Had you used a foreach loop instead of map you could eliminate one of the copies:

my @aDone; foreach (@aUnNormalized) { push @aPending, $_; my @aNormalized; while (scalar(@aPending)) { my $sPending = shift @aPending; my $sNormalized = normalize($sPending, [@aPending]); if (!defined($sNormalized)) { #not enough information to normalize yet unshift @aPending, $sPending; last; } push @aDone, $sNormalized; } }

Best, beth


In reply to Re: Counting elements being mapped via map{} by ELISHEVA
in thread Counting elements being mapped via map{} by furry_marmot

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.