Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #203443 > unrolled thread

handling lists in perl

Started bymick crane <mick.crane@gmail.com>
First post2018-12-18 14:40 +0100
Last post2018-12-18 15:00 +0100
Articles 17 — 6 participants

Back to article view | Back to linux.debian.user


Contents

  handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-18 14:40 +0100
    Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-18 14:50 +0100
      Re: handling lists in perl deloptes <deloptes@gmail.com> - 2018-12-18 20:10 +0100
        Re: handling lists in perl deloptes <deloptes@gmail.com> - 2018-12-19 01:50 +0100
          Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-19 05:40 +0100
          Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-19 06:10 +0100
            Re: handling lists in perl deloptes <deloptes@gmail.com> - 2018-12-19 08:20 +0100
              Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-19 09:30 +0100
                Re: handling lists in perl deloptes <deloptes@gmail.com> - 2018-12-19 09:40 +0100
                  Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-19 10:20 +0100
                    Re: handling lists in perl mick crane <mick.crane@gmail.com> - 2018-12-19 10:30 +0100
                      Re: handling lists in perl Andy Smith <andy@strugglers.net> - 2018-12-19 18:00 +0100
                        Re: handling lists in perl deloptes <deloptes@gmail.com> - 2018-12-19 20:00 +0100
                        Re: handling lists in perl David Wright <deblis@lionunicorn.co.uk> - 2018-12-19 21:50 +0100
                          Re: handling lists in perl Greg Wooledge <wooledg@eeg.ccf.org> - 2018-12-19 22:10 +0100
      Re: handling lists in perl David Christensen <dpchrist@holgerdanske.com> - 2018-12-19 03:40 +0100
    Re: handling lists in perl Greg Wooledge <wooledg@eeg.ccf.org> - 2018-12-18 15:00 +0100

#203443 — handling lists in perl

Frommick crane <mick.crane@gmail.com>
Date2018-12-18 14:40 +0100
Subjecthandling lists in perl
Message-ID<x6nLj-4bW-5@gated-at.bofh.it>
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 15.... )
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.

mick



-- 
Key ID    4BFEBB31

[toc] | [next] | [standalone]


#203444

Frommick crane <mick.crane@gmail.com>
Date2018-12-18 14:50 +0100
Message-ID<x6nUZ-4fy-3@gated-at.bofh.it>
In reply to#203443
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.
-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203453

Fromdeloptes <deloptes@gmail.com>
Date2018-12-18 20:10 +0100
Message-ID<x6sUF-7sK-11@gated-at.bofh.it>
In reply to#203444
Hi,

it is not exactly clear if you want to test the key or the value of the
pairs, but I assume you are after the values.

mick crane wrote:

> I have an list of pairs
> (1=>8,2=>20,6=>100,15=>100....)

this would be a hash array (means key/value pairs)

> and an array of unique numbers
> (1 21 100 8 15 22 6 12 56.... )

This is a standard/classic array

> 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.

so iterate over the hash and check for matches in the array

my %hash = ( 1=>8,2=>20,6=>100,15=>100 );
my @array = [1, 21, 100, 8, 15, 22, 6, 12, 56];

foreach my $key (keys %hash) {
        if ( @array =~ /$hash[$key]/) {
                print "key $key with value " . $hash{$key} .
                        " is in the array of values\n";
        }
}

result
key 6 with value 100 is in the array of values
key 2 with value 20 is in the array of values
key 15 with value 100 is in the array of values
key 1 with value 8 is in the array of values

regards

[toc] | [prev] | [next] | [standalone]


#203462

Fromdeloptes <deloptes@gmail.com>
Date2018-12-19 01:50 +0100
Message-ID<x6ydI-26h-5@gated-at.bofh.it>
In reply to#203453
deloptes wrote:

> if ( @array =~ /$hash[$key]/) {
> print "key $key with value " . $hash{$key} .
> " is in the array of values\n";
> }

I checked and it seems the right answer is

my %hash = ( 1=>8,2=>20,6=>100,15=>100 );
my @array = (1, 21, 100, 8, 15, 22, 6, 12, 56);

foreach my $key (keys %hash) {
        for (  @array ) {
                if ($_ eq $hash{$key} ) {
                        print "key $key with value " . $hash{$key} .
                                " is in the array of values\n";
                }
        }
}

[toc] | [prev] | [next] | [standalone]


#203465

Frommick crane <mick.crane@gmail.com>
Date2018-12-19 05:40 +0100
Message-ID<x6BOh-4qh-1@gated-at.bofh.it>
In reply to#203462
On 2018-12-19 00:43, deloptes wrote:
> deloptes wrote:
> 
>> if ( @array =~ /$hash[$key]/) {
>> print "key $key with value " . $hash{$key} .
>> " is in the array of values\n";
>> }
> 
> I checked and it seems the right answer is
> 
> my %hash = ( 1=>8,2=>20,6=>100,15=>100 );
> my @array = (1, 21, 100, 8, 15, 22, 6, 12, 56);
> 
> foreach my $key (keys %hash) {
>         for (  @array ) {
>                 if ($_ eq $hash{$key} ) {
>                         print "key $key with value " . $hash{$key} .
>                                 " is in the array of values\n";
>                 }
>         }
> }


much appreciated, cheers.

mick

-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203466

Frommick crane <mick.crane@gmail.com>
Date2018-12-19 06:10 +0100
Message-ID<x6Chj-4Q1-7@gated-at.bofh.it>
In reply to#203462
On 2018-12-19 00:43, deloptes wrote:
> deloptes wrote:
> 
>> if ( @array =~ /$hash[$key]/) {
>> print "key $key with value " . $hash{$key} .
>> " is in the array of values\n";
>> }
> 
> I checked and it seems the right answer is
> 
> my %hash = ( 1=>8,2=>20,6=>100,15=>100 );
> my @array = (1, 21, 100, 8, 15, 22, 6, 12, 56);
> 
> foreach my $key (keys %hash) {
>         for (  @array ) {
>                 if ($_ eq $hash{$key} ) {
>                         print "key $key with value " . $hash{$key} .
>                                 " is in the array of values\n";
>                 }
>         }
> }

except there is only one 100 in @array it gets me along.
thanks and David too.

mick

-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203467

Fromdeloptes <deloptes@gmail.com>
Date2018-12-19 08:20 +0100
Message-ID<x6Ej7-64E-5@gated-at.bofh.it>
In reply to#203466
mick crane wrote:

> except there is only one 100 in @array it gets me along.
> thanks and David too.

but this one 100 satisfies both 6 and 15 so 6 and 15 match.

regards

[toc] | [prev] | [next] | [standalone]


#203469

Frommick crane <mick.crane@gmail.com>
Date2018-12-19 09:30 +0100
Message-ID<x6FoS-6Px-5@gated-at.bofh.it>
In reply to#203467
On 2018-12-19 07:17, deloptes wrote:
> mick crane wrote:
> 
>> except there is only one 100 in @array it gets me along.
>> thanks and David too.
> 
> but this one 100 satisfies both 6 and 15 so 6 and 15 match.
> 
> regards

yes but there is only one 100, only one key can have it.
I still try to figure out what goes on with the hash pairs
with a longer list of numbers your example seems to pick up on the 
values somehow.
I dunno, unless there is something about using $_ in a loop being 
unreliable ?
I persevere to succeed
=o)


mick
-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203470

Fromdeloptes <deloptes@gmail.com>
Date2018-12-19 09:40 +0100
Message-ID<x6Fyx-6SG-1@gated-at.bofh.it>
In reply to#203469
mick crane wrote:

> yes but there is only one 100, only one key can have it.
> I still try to figure out what goes on with the hash pairs
> with a longer list of numbers your example seems to pick up on the
> values somehow.
> I dunno, unless there is something about using $_ in a loop being
> unreliable ?
> I persevere to succeed

(1=>8,2=>20,6=>100,15=>100....)

this is what you originally posted, so both 6 and 15 match 100 from the list

when key is 6 it compares 100 with each element from the list and matches
100. Same goes for 15.
No idea what you mean!

regards

[toc] | [prev] | [next] | [standalone]


#203471

Frommick crane <mick.crane@gmail.com>
Date2018-12-19 10:20 +0100
Message-ID<x6Gbf-7lJ-7@gated-at.bofh.it>
In reply to#203470
On 2018-12-19 08:34, deloptes wrote:
> mick crane wrote:
> 
>> yes but there is only one 100, only one key can have it.
>> I still try to figure out what goes on with the hash pairs
>> with a longer list of numbers your example seems to pick up on the
>> values somehow.
>> I dunno, unless there is something about using $_ in a loop being
>> unreliable ?
>> I persevere to succeed
> 
> (1=>8,2=>20,6=>100,15=>100....)
> 
> this is what you originally posted, so both 6 and 15 match 100 from the 
> list
> 
> when key is 6 it compares 100 with each element from the list and 
> matches
> 100. Same goes for 15.
> No idea what you mean!
> 
> regards


given a set of *unique* numbers ( there is just one of the numbers in 
the list)
and a list of possible pairings find which pairs you can make from the 
list.
6 can pair with 100 and 15 can pair with 100 but they cannot both be in 
the list of possible pairings because there is only one 100.




-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203473

Frommick crane <mick.crane@gmail.com>
Date2018-12-19 10:30 +0100
Message-ID<x6GkV-7pe-7@gated-at.bofh.it>
In reply to#203471
On 2018-12-19 09:12, mick crane wrote:
> On 2018-12-19 08:34, deloptes wrote:
>> mick crane wrote:
>> 
>>> yes but there is only one 100, only one key can have it.
>>> I still try to figure out what goes on with the hash pairs
>>> with a longer list of numbers your example seems to pick up on the
>>> values somehow.
>>> I dunno, unless there is something about using $_ in a loop being
>>> unreliable ?
>>> I persevere to succeed
>> 
>> (1=>8,2=>20,6=>100,15=>100....)
>> 
>> this is what you originally posted, so both 6 and 15 match 100 from 
>> the list
>> 
>> when key is 6 it compares 100 with each element from the list and 
>> matches
>> 100. Same goes for 15.
>> No idea what you mean!
>> 
>> regards
> 
> 
> given a set of *unique* numbers ( there is just one of the numbers in 
> the list)
> and a list of possible pairings find which pairs you can make from the 
> list.
> 6 can pair with 100 and 15 can pair with 100 but they cannot both be
> in the list of possible pairings because there is only one 100.

just to be a bit clearer.
given a list A of *unique* numbers
and a list B of possible pairings find which pairs you can make from the 
list A.
6 can pair with 100 and 15 can pair with 100 but they cannot both be
in the list of possible pairings because there is only one 100.




-- 
Key ID    4BFEBB31

[toc] | [prev] | [next] | [standalone]


#203474

FromAndy Smith <andy@strugglers.net>
Date2018-12-19 18:00 +0100
Message-ID<x6Nmp-33G-3@gated-at.bofh.it>
In reply to#203473
Hi Mick,

On Wed, Dec 19, 2018 at 09:23:59AM +0000, mick crane wrote:
> just to be a bit clearer.
> given a list A of *unique* numbers
> and a list B of possible pairings find which pairs you can make from the
> list A.
> 6 can pair with 100 and 15 can pair with 100 but they cannot both be
> in the list of possible pairings because there is only one 100.

You don't have two lists though; you have a list and a hash. By
using the correct Perl terminology it will become a lot clearer what
you want to achieve.

You have a LIST of numbers.

You have a HASH that contains KEYS and VALUES.

Do you want to match the KEYS from the HASH with the numbers in the
LIST, or the VALUES from the HASH with the numbers from the LIST?
And do you want to return what was matched or the KEYS of what was
matched?

Since the HASH can have multiple instances of the same VALUE but
with different KEYS, if you are matching the VALUE against something
what do you want to do when there is more than one instance of the
same VALUE?

You saying, "a list B of possible pairings find which pairs you can
make from the list A" doesn't help when B is actually a hash,
because hashes have keys and values, so we don't know if you mean to
"make pairings" with the key or the value, nor is it entirely clear
what "make a pairing" means. We can guess you mean "is equal to"
once we know which thing you want it to be equal to, but it's still
a guess. Why make us guess? Just say "check for equality" or
something if that is what you mean. :)

Cheers,
Andy

-- 
https://bitfolk.com/ -- No-nonsense VPS hosting

[toc] | [prev] | [next] | [standalone]


#203477

Fromdeloptes <deloptes@gmail.com>
Date2018-12-19 20:00 +0100
Message-ID<x6Pex-4at-5@gated-at.bofh.it>
In reply to#203474
Andy Smith wrote:

> You saying, "a list B of possible pairings find which pairs you can
> make from the list A" doesn't help when B is actually a hash,
> because hashes have keys and values, so we don't know if you mean to
> "make pairings" with the key or the value, nor is it entirely clear
> what "make a pairing" means. We can guess you mean "is equal to"
> once we know which thing you want it to be equal to, but it's still
> a guess. Why make us guess? Just say "check for equality" or
> something if that is what you mean. :)

+1

and I think he is coming from game theory corner, given the terminology
used.
Anyway as I said in the first comment it is not exactly clear what OP
wanted. Now we know what OP wants, but it is not clear if it makes sense. I
think this example is ill in itself as even now as you say it is not clear
what should happen with the second match of 100 and why 6 should have
precedence over 15.

[toc] | [prev] | [next] | [standalone]


#203479

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2018-12-19 21:50 +0100
Message-ID<x6QX0-5eB-17@gated-at.bofh.it>
In reply to#203474
On Wed 19 Dec 2018 at 16:53:44 (+0000), Andy Smith wrote:
> On Wed, Dec 19, 2018 at 09:23:59AM +0000, mick crane wrote:
> > just to be a bit clearer.
> > given a list A of *unique* numbers
> > and a list B of possible pairings find which pairs you can make from the
> > list A.
> > 6 can pair with 100 and 15 can pair with 100 but they cannot both be
> > in the list of possible pairings because there is only one 100.
> 
> You don't have two lists though; you have a list and a hash. By
> using the correct Perl terminology it will become a lot clearer what
> you want to achieve.
> 
> You have a LIST of numbers.
> 
> You have a HASH that contains KEYS and VALUES.

I don't think the "pairs" form a hash/dictionary; I think they're just
ordered pairs of numbers, better expressed as a tuple. (Sorry to use
python jargon; I stopped using perl in the last millennium.) So we
have a list of tuples and a list of numbers.

> Do you want to match the KEYS from the HASH with the numbers in the
> LIST, or the VALUES from the HASH with the numbers from the LIST?
> And do you want to return what was matched or the KEYS of what was
> matched?
> 
> Since the HASH can have multiple instances of the same VALUE but
> with different KEYS, if you are matching the VALUE against something
> what do you want to do when there is more than one instance of the
> same VALUE?
> 
> You saying, "a list B of possible pairings find which pairs you can
> make from the list A" doesn't help when B is actually a hash,
> because hashes have keys and values, so we don't know if you mean to
> "make pairings" with the key or the value, nor is it entirely clear
> what "make a pairing" means. We can guess you mean "is equal to"
> once we know which thing you want it to be equal to, but it's still
> a guess. Why make us guess? Just say "check for equality" or
> something if that is what you mean. :)

I assume that for two numbers in the second list to be selected, both
have to match the two members of an individual tuple. As the tuples
are ordered, I would assume that they are to be tested in the order
supplied.

As dictionary lookups are fast, I would convert the second list into
a dictionary: the number as the dictionary key and the number of
occurrences as the dictionary value. (Although the problem states
that the second list shouldn't have duplicates, this method checks
whether this is indeed the case.)

Scanning the list of tuples in order, it's now quick to see if both
members are in the dictionary and, if so, decrement their values or
delete. What survives of the dictionary keys is the unmatched numbers;
the matched pairs can be added to a list, individually or as tuples.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#203480

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-12-19 22:10 +0100
Message-ID<x6Rgl-5BH-7@gated-at.bofh.it>
In reply to#203479
On Wed, Dec 19, 2018 at 02:48:17PM -0600, David Wright wrote:
> I assume that for two numbers in the second list to be selected, both
> have to match the two members of an individual tuple. As the tuples
> are ordered, I would assume that they are to be tested in the order
> supplied.
> 
> As dictionary lookups are fast, I would convert the second list into
> a dictionary: the number as the dictionary key and the number of
> occurrences as the dictionary value. (Although the problem states
> that the second list shouldn't have duplicates, this method checks
> whether this is indeed the case.)
> 
> Scanning the list of tuples in order, it's now quick to see if both
> members are in the dictionary and, if so, decrement their values or
> delete. What survives of the dictionary keys is the unmatched numbers;
> the matched pairs can be added to a list, individually or as tuples.

Yup, this was my interpretation as well.  He's got what ought to be a
hash stored as a list, and he's got what ought to be a list of lists
stored as a hash.  And we don't know what any of it represents, or
what he wants to do with them, or WHY.

OP, you need to provide an actually readable version of the problem.
Explain what you're doing.  Tell us what your inputs ARE and where
they COME FROM and what you want to DO with them.

Ideally, give actual examples.  Not made-up bullshit examples.  REAL ones.

[toc] | [prev] | [next] | [standalone]


#203463

FromDavid Christensen <dpchrist@holgerdanske.com>
Date2018-12-19 03:40 +0100
Message-ID<x6zW9-3bV-1@gated-at.bofh.it>
In reply to#203444
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

[toc] | [prev] | [next] | [standalone]


#203445

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2018-12-18 15:00 +0100
Message-ID<x6o4F-4iV-15@gated-at.bofh.it>
In reply to#203443
On Tue, Dec 18, 2018 at 01:34:53PM +0000, mick crane wrote:
> 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 15.... )
> 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.

Start by converting the "array of unique numbers" to a hash (associative
array, dictionary).  That way, you can quickly look up which numbers are
in, or not in, the data set.

Next, set up a second hash to record which numbers have been matched (vs.
which have not) in the following step.  It starts out empty.

Now, iterate over your "pairs".  If both members of the "pair" are
in the first hash, then write the pair to the appropriate output bin,
and add both numbers to the second hash.

When that's finished, you want to find out which numbers from the first
hash are not present in the second hash.  So, iterate over the keys of
the first hash, and any key that is not also in the second hash, write
to the appropriate output bin.

This can be done in any language that has hashes (associative arrays,
dictionaries) and the ability to iterate over a list.  Perl certainly
qualifies, if that's your language of choice.  Python and Tcl can also
do it quite easily.  Even bash could do this, although it would be slow,
and would not be my first pick.

If you need help actually *writing* it in Perl (or whatever language),
you should ask for help in a language-specific mailing list.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web