There have been some posts lately, inspired by Project Euler's problems. I also play with PE, although on a sparse basis and having joined much late.

In a particular problem it turned out to be of much use Perl's ability to return undef when accessing an array element beyond its actual length, and the promotion of undef to zero in a numeric addition. Except that it triggers the well know warning, in which case the usual cure is to locally disable it. (Since the actual script body was 14 lines, I eventually committed for once the sin of commenting out the use warnings; line altogether.)

Of course I may have used a (IMHO) visually obtrusive ||0 instead...

The point is, in this case the intended usage is more in line with common mathematical one, especially as in combinatorics. As an example consider the following minimal implementation of a program to print the first ten rows of Pascal's triangle:

#!/usr/bin/perl -l use strict; # use warnings; my @row; while ($row[1]<10) { @row=(1, map $row[$_-1]+$row[$_], 1..@row); print "@row"; } __END__

If I enable warnings I'll get two for the comparison for the first two iterations and one for the addition per each row.

So I wonder if one day a predefined attribute may be specified on an array so that accessing it beyond its limits would return 0 rather than undef. Perhaps something like

my @row :zero;

Also, Perl's current behaviour with negative indices is occasionally practical but also not best suited for some mathematical-like usages. So still in the brainstorming vein as above, perhaps another attribute should exist, say :nocirc, to the effect of returning undef (or 0 if in conjunction with :zero) when accessing negative indices. Thus the code above may become the clearer - for the mathematically inclined:

my @row :zero :nocirc; while ($row[1]<10) { @row=map $row[$_-1]+$row[$_], 0..@row; print "@row"; }

Admittedly, this is not Perl's everyday job. Still it may be useful. I would add this to ysth's 5.12 wishlist.


In reply to A pair of "mathematical" attributes for arrays? by blazar

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.