Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found