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

Discussion in 'Perl Misc' started by vlsidesign, Mar 18, 2005.

  1. vlsidesign

    vlsidesign Guest

    -- 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
     
    vlsidesign, Mar 18, 2005
    #1
    1. Advertising

  2. vlsidesign <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 18, 2005
    #2
    1. Advertising

  3. vlsidesign

    Joel Graber Guest

    "vlsidesign" <> writes:

    > 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.
    --
    Joel
     
    Joel Graber, Mar 18, 2005
    #3
  4. vlsidesign

    vlsidesign Guest

    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??
     
    vlsidesign, Mar 19, 2005
    #4
  5. vlsidesign

    Bob Walton Guest

    vlsidesign wrote:

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

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Mar 20, 2005
    #5
  6. vlsidesign

    vlsidesign Guest

    *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.
     
    vlsidesign, Mar 21, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Navin
    Replies:
    1
    Views:
    704
    Ken Schaefer
    Sep 9, 2003
  2. colin_lyse
    Replies:
    1
    Views:
    152
    Tore Aursand
    Feb 3, 2005
  3. PerlFAQ Server
    Replies:
    0
    Views:
    200
    PerlFAQ Server
    Feb 5, 2011
  4. Jose Luis

    Perl sort different from unix sort

    Jose Luis, Mar 3, 2011, in forum: Perl Misc
    Replies:
    3
    Views:
    362
    Steve C
    Mar 3, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    170
    PerlFAQ Server
    Mar 13, 2011
Loading...

Share This Page