Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: An easier way to construct lists of numbers?

by Chady (Priest)
on Jan 24, 2004 at 14:59 UTC ( #323833=note: print w/replies, xml ) Need Help??

in reply to An easier way to construct lists of numbers?

Your code does not run and has a lot of errors, anyway, if I understand your code correctly, here's something that will work for you.

use Data::Dumper; # note that you do not need to explicitly fill in the # elements of the array one by one, make use of # perl's lists. my @lite = ( [qw/2 5 0 999 999 0/], [qw/4 3 0 999 999/], [qw/1 4 0 999 999/], [qw/1 4 0 999 999/], [qw/4 1 0 999 999/] ); my %h; foreach (@lite) { # construct a key made of the X value x the Y # value and count on that being uniq. my $xy = $_->[0] . 'x' . $_->[1]; # hash keys are uniq, # so we make the value an array reference # containing the original x and y values. $h{$xy} = [$_->[0], $_->[1]]; } # just retrieve the values from the hash. my @filtered = values %h; print Dumper(\@filtered);

He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady |

Replies are listed 'Best First'.
Re: Re: An easier way to construct lists of numbers?
by Skeeve (Vicar) on Jan 24, 2004 at 15:35 UTC
    I think he just wants to search the numbers and doesn't care whether it's x or y. So your hash asignment becomes:
    ++$h{$_->[0]}; ++$h{$_->[1]};
    and the result is simply printed with:
    print join(' ',sort keys %h),"\n";
Re: Re: An easier way to construct lists of numbers?
by yosefm (Friar) on Jan 24, 2004 at 15:39 UTC
    Very nice, Chady.

    I'd like to add a few things:

    First, to filter out the unused ($lite[$something][5]) you can change:

    $h{$xy} = [$_->[0], $_->[1]];
    $h{$xy} = [$_->[0], $_->[1]] if $_->[5];

    Second, when you write a loop, you should try to include only the essential stuff in it, so your program runs faster and consumes less resources. so this:

    for my $o (0 .. $#line) { select BLA print ... select STDOUT }

    can become:
    select BLA; for my $o (0 .. $#line) { print ... } select STDOUT;
    But it's also possible to give a file handle to print, like so: print HANDLE "Something\n"; and then you don't have to select and reselect at-all.

    perl -e'$b=unpack"b*",pack"H*","59dfce2d6b1664d3b26cd9969503";\ for(;$a<length$b;$a+=9){print+pack"b8",substr$b,$a,8;}'
    My public key

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://323833]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-06-20 04:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.