Greetings, all. I inherited some Perl code. But before I go too far with it, I wanted to get better familiar with it.

It's a Perl application. But has a bit of JavaScript included. The following is a JavasScript function. But given that Perl, and JavaScript are so similar, in so many ways. I was hoping you all might indulge me.

'function' : function (fieldObj,option) { var v = fieldObj.value = fieldObj.value.toString().replace(/\s/g,'').toUpperCase(); if (!v) { return false; } var bm = v.match(/^JM-\d+-\S{4}[1-9Zz]{6}$/); if ( v.match(/^JM-\d+-\S{4}[1-9Zz]{6}$/) ) { return true; } var map = {}; var dv = 0; $w('A B C D E F G H J K L M N P Q R T U V W Y 3 4 6 7 8 9').each( function(d){ map[d] = dv++; } ); var chars = v.toArray().reverse(); var check = chars.shift(); chars = chars.findAll(function(d){ return map[d] != null }); if (chars.length != 19) { return false; } var totalVal = 0; var flip = 1; chars.each(function(c){ var posVal = map[c]; if ( !(flip = !flip) ) { posVal *=2 } while(posVal){ totalVal = totalVal + (posVal % 10); posVal = Math.floor(posVal/10); } }); return ( check == ( 10 - totalVal % 10 ) % 10 ); }

It's a form field validation function, and requires certain input. But I'm afraid it's a bit over my head, and was hoping one of the brighter Monks here, might help me better understand it.

It seems quite clever, but either my math, or my code tracing isn't quite up to snuff, on this one.

Thank you for all your time, and consideration.

--Chris

¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH


In reply to I'm struggling with a function, and hoping for some insight -- not strictly Perl by taint

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.