Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How can one find out the pairwise difference and product between elements in a perl array without using a perl module?

by Anonymous Monk
on Sep 14, 2013 at 01:58 UTC ( #1054044=note: print w/ replies, xml ) Need Help??


in reply to How can one find out the pairwise difference and product between elements in a perl array without using a perl module?

Not as fancy as some, but gives the output you specified. O might not be too bad as it's only a single pass through for the main number, and both operations can be done on the same pass. As always, YMMV.

#!/usr/bin/perl use strict; use warnings; my @array=qw/a b c d/; while ( @array > 1 ) { my $first = shift(@array); for my $next ( @array ) { # take diff $first - $next; # take product $first * $next; print sprintf("%-8s%s\n","$first - $next","$first * $next"); } } exit; __END__ a - b a * b a - c a * c a - d a * d b - c b * c b - d b * d c - d c * d


Comment on Re: How can one find out the pairwise difference and product between elements in a perl array without using a perl module?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054044]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-07-13 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls