Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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?
[dazz]: Hi I have grabbed an image from an IP camera with Image::Grab. I then want to pass that image object to a subroutine where I do a read-only test on the image with Image:Magick.
[dazz]: How do I pass an image object to a subroutine and then apply a Image::Magick a read-only method to the image object???
[dazz]: Also, I want to use Image::Magick to annotate and composite (not in a sub). How do I take a Image::Grab object and apply Image::Magick write methods to it??
[dazz]: At present, I am saving the Image::Grab image to disk, then creating a new Image::Magick object that reads the disk file.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-03-27 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (317 votes). Check out past polls.