I have a list of x and y values. I am trying to make a list of all the numbers (x's and y's) without repeating. I have very crude code, and I was looking for suggestions. It does what I want it to do, but I would like to make it a tad bit more dilagent. I would also like to get rid of the extra entries in the array @line. The final ones that do not get used have the value of zero, and there is not really a need for them. Thanks anyone who can help.
use strict; use warnings; use Data::Dumper; open ( OHA, ">c:/printoutLIST1.txt" ) or die ("could not open file. &! +" ); my @lite; my ($x, $y); # $lite[0][0] = x value # $lite[0][1] = y value # $lite[0][2] = (used?) 0 = not used, 1 = used # $lite[0][3] = x location in grid (lower left corner) # $lite[0][4] = y location in grid (lower left corner) # $lite[0][5] = (rotated?) 0 = not rotated, 1 = rotated $lite[0][0] = 2; $lite[0][1] = 5; $lite[0][2] = 0; $lite[0][3] = 999; $lite[0][4] = 999; $lite[0][5] = 0; $lite[1][0] = 4; $lite[1][1] = 3; $lite[1][2] = 0; $lite[1][3] = 999; $lite[1][4] = 999; $lite[2][0] = 1; $lite[2][1] = 4; $lite[2][2] = 0; $lite[2][3] = 999; $lite[2][4] = 999; $lite[3][0] = 4; $lite[3][1] = 1; $lite[3][2] = 0; $lite[3][3] = 999; $lite[3][4] = 999; my @line; for my $i (0..2*$#lite) { $line[$i] = 0; } for my $j (0..$#lite) { # loop through all possibilities for my $k (0..$#line) { # check if x is already in the list if ($lite[$j][0] == $line[$k]) { # if x is in list-get out goto OUT; } } for my $l (0..$#line) { # find where to place x if ($line[$l] == 0) { $x = $l; goto OUT2; } } OUT2: $line[$x] = $lite[$j][0]; OUT: for my $m (0..$#line) { # check if y is already in list if ($lite[$j][1] == $line[$m]) { # if y is in list-get out goto OUT3; } } for my $n (0..$#line) { # find where to place y if ($line[$n] == 0) { $y = $n; goto OUT4; } } OUT4: $line[$y] = $lite[$j][1]; OUT3: } for my $p (0..$#line) { for my $o (0..$#line) { select OHA; print "LINE $o [$line[$o]]\n"; select STDOUT; }

20040124 Edit by BazB: Changed title from 'there must be an easier way'


In reply to An easier way to construct lists of numbers? by stu96art

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.