Re: handling lists in perl
On 12/18/18 5:43 AM, mick crane wrote:
On 2018-12-18 13:34, mick crane wrote:
sorry I put 15 twice there just to confuse the issue
should be
not really on the topic but...
I'm not very good at perl (or anything else ) and could maybe sort it
but perhaps there is an extension that does it.
I have an list of pairs
(1=>8,2=>20,6=>100,15=>100....)
and an array of unique numbers
(1 21 100 8 15 22 6 12 56.... )
I want to see what pairs can be satisfied from the array of numbers,
send that list to a file and also to another file the left over numbers.
The three canonical Perl (version 5) books are:
1. Learning Perl -- gets you up the initial learning curve:
http://shop.oreilly.com/product/0636920049517.do
2. Perl Cookbook -- "real world" code examples with explanations:
http://shop.oreilly.com/product/9780596003135.do
3. Programming Perl -- the language reference:
http://shop.oreilly.com/product/9780596004927.do
I learned Perl via the first two. I didn't get into the third until I
was well on my way.
If you just want to "get it done", here is a starter script:
2018-12-18 18:26:48 dpchrist@tinkywinky ~/sandbox/perl/debian-users
$ cat 20181218-1334-mick-crane.pl
#!/usr/bin/env perl
use strict;
use warnings;
my %pairs = (1 => 8, 2 => 20, 6 => 100, 15 => 100);
my @unique = (1, 21, 100, 8, 15, 22, 6, 12, 56);
open (my $fh_satisfies, ">satisfies.txt") or die $!;
open (my $fh_leftovers, ">leftovers.txt") or die $!;
for my $k (@unique) {
my $fh = exists $pairs{$k} ? $fh_satisfies : $fh_leftovers;
$fh->print($k, "\n");
}
2018-12-18 18:28:09 dpchrist@tinkywinky ~/sandbox/perl/debian-users
$ perl 20181218-1334-mick-crane.pl
2018-12-18 18:28:22 dpchrist@tinkywinky ~/sandbox/perl/debian-users
$ cat satisfies.txt
1
15
6
2018-12-18 18:28:26 dpchrist@tinkywinky ~/sandbox/perl/debian-users
$ cat leftovers.txt
21
100
8
22
12
56
David
Reply to: