Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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 imbibing at the Monastery: (8)
As of 2015-07-04 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls