Perl program help to sort of 'pad name xy' file

V

vlsidesign

-- Looking for ideas to get me going --

I want to 'sort lines of file based on a numeric field of each line'.
I was looking for some ideas on how to approach this.

Basically, each line in the file is in this format:
pad_cell xcoord ycoord label_name

For example,
pad_cell 30 30 vdd
pad_cell 30 1000 vss
pad_cell 30 500 data1
pad_cell 60 30 vdd
pad_cell 500 30 vss
pad_cell 350 30 data2
etc....

Program usage: I am trying to sort a computer chip pad order list in a
file. My sort is basically done by assign top left corner as PAD1 and
going around counter clockwise. Anyway, the file I have is out of order
and I want to sort it.

------------------------------------------------------------------------------
P.S.
I am familiar with basics like split, join, regex (using parenthesis to
retain parts in memory). Anything more exotic needed I am willing to
lookup and study in an Oreilly book or elsewhere. However, I am just on
limited time schedule, and wanted to get good direction first. I
haven't used PERL yet for numeric sorting (I usually use the plan unix
'sort' utility, but I think Perl will better handle this case
 
T

Tad McClellan

vlsidesign said:
I want to 'sort lines of file based on a numeric field of each line'.
I was looking for some ideas on how to approach this.


perldoc -f sort

and

perldoc -q sort

How do I sort an array by (anything)?
How do I sort a hash (optionally by value instead of key)?
How can I always keep my hash sorted?
 
J

Joel Graber

vlsidesign said:
I want to 'sort lines of file based on a numeric field of each line'.
I was looking for some ideas on how to approach this.

Basically, each line in the file is in this format:
pad_cell xcoord ycoord label_name

For example,
pad_cell 30 30 vdd
pad_cell 30 1000 vss
pad_cell 30 500 data1
pad_cell 60 30 vdd
pad_cell 500 30 vss
pad_cell 350 30 data2
etc....

Program usage: I am trying to sort a computer chip pad order list in a
file. My sort is basically done by assign top left corner as PAD1 and
going around counter clockwise. Anyway, the file I have is out of order
and I want to sort it.

You have a set of randomly ordered x,y coordinates that form the shape of
a square hollow frame, and you want to order them counter clockwise
from upper left corner?
I assume you mean start from the top of the left edge.

One approach I have used in the past for this exact
problem set, is to convert the x,y coordinates into
polar coordinates, and sort by angle from the center.
If you have more than one row of pads per side,
this may not work well in the corners.

The other more generic approach is to partition them into sides,
then sort each side in the correct order,
then concatenate the sides in the correct order.
For numeric sorting see also page 218 "sort numerically"
in the blue camel book.
 
V

vlsidesign

Thanks Joel for your response.

Your assumption was correct, I would start from top-left corner. The
pads are not staggered so it sounds like it would work beautifully.
Your suggestion sounds very cool, and clever.

I did a google search and some reading so here goes, sorry if it is
crude and not quite accurate... So if O is the origin (0,0) of my x y
coordinate plane. I can take Pythagorems theorem for the radius r. So
r = to the square root of (x^2 + y^2). Also if v = theta (angle) then
y = r sin v, x = r cos v, y/x=tan v, so v = tan-1(y/x).

Is that correct? I then could sort v (which is the angle) in PERL from
there??
 
B

Bob Walton

vlsidesign said:
Thanks Joel for your response.

Your assumption was correct, I would start from top-left corner. The
pads are not staggered so it sounds like it would work beautifully.
Your suggestion sounds very cool, and clever.

I did a google search and some reading so here goes, sorry if it is
crude and not quite accurate... So if O is the origin (0,0) of my x y
coordinate plane. I can take Pythagorems theorem for the radius r. So
r = to the square root of (x^2 + y^2). Also if v = theta (angle) then
y = r sin v, x = r cos v, y/x=tan v, so v = tan-1(y/x).

Is that correct? I then could sort v (which is the angle) in PERL from
there??

Yep, you've got it. You might note that Perl has builtin the
atan2(y,x) function which computes the four-quadrant arctan(y/x)
correctly, in radians.
 
V

vlsidesign

*Thanks a lot Bob for the atan2 function, the function is what I
needed. I just need to read up on Perl's numeric sorting, and I think
that'll about do the trick.
*Thanks again Joel, this is going to work better than the more typical
solution.
* Also thanks Tad, I'll follow up on those perldocs when I get to work
tomorrow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top