Are you looking for all unique combinations? If so, here you go...

use strict; use warnings; use Math::Combinatorics; # Get the data file name from command line. # Output filename is optional. my( $infile, $outfile ) = @ARGV; die "No filenames provided.\n" unless defined $infile; $outfile = ( defined $outfile ) ? $outfile : $infile . '.out'; # Read input data and put it in an array for later use. open my $in_handle, '<', $infile or die "Couldn't open $infile for input.\n$!"; my $dataset_aref; while ( my $number = <$in_handle> ) { chomp $number; next unless length $number > 0; push @{$dataset_aref}, $number; } close $in_handle; # Create a Math::Combinatorics object. my $combinat = Math::Combinatorics->new( count => 2, data => $dataset_aref, ); # Open an output file, and print out the combinations. open my $out_handle, '>', $outfile or die "Couldn't open $outfile for output.\n$!"; while ( my @combo = $combinat->next_combination ) { print $out_handle "@combo\n"; } close $out_handle or die "Couldn't close $outfile after output.\n$!";

Usage example:

perl mycombinat.pl data.txt data.out

data.txt should contain a '\n' delimited list of elements. data.out will contain a list of pairings. If you don't specify an output filename one will be chosen for you (the extension '.out' will be added to whatever input filename you specified).

This script uses Math::Combinatorics to find all unique combinations, two at a time. nC2.


Dave


In reply to Re: help writing simple perl script by davido
in thread help writing simple perl script by ashylarry

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.