Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort List 2

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
I have a text file with a long list of values in it. The values are actually for coordinates, the values are like this;

X0Y0 First Value
X0Y1 Second Value
X0Y2 etc.
X1Y0
X1Y1
X1Y2
...
...
X15Y0
X15Y1
X15Y2
etc.

I know the bounds of X and Y and I want to rearrange it and put into a new file looking like this;

X0Y0 X0Y1 X0Y2
X1Y0 X1Y1 X1Y2
X2Y0 X2Y1 X2Y2
etc.

I would guess it's few lines of code but I'm getting nowhere. I know the X and Y bounds before I start btw.

Any help appreciated.
 
Do you mean something like this?
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@points[/color] = ();
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$i[/color] = [COLOR=#ff00ff]0[/color];
[COLOR=#804040][b]while[/b][/color]([COLOR=#008080]<DATA>[/color]){
 [COLOR=#804040][b]chomp[/b][/color];
 [COLOR=#008080]$i[/color]++;
 [COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@points[/color], [COLOR=#008080]$_[/color];
 [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$i[/color] == [COLOR=#ff00ff]3[/color]) {
   [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$points_line[/color] = [COLOR=#804040][b]join[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff] [/color][COLOR=#ff00ff]"[/color], [COLOR=#008080]@points[/color]);
   [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$points_line[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
   [COLOR=#0000ff]# initialize[/color]
   [COLOR=#008080]$i[/color] = [COLOR=#ff00ff]0[/color];
   [COLOR=#008080]@points[/color]=();
 }
}

[COLOR=#0000ff]__DATA__[/color]
[COLOR=#0000ff]X0Y0[/color]
[COLOR=#0000ff]X0Y1[/color]
[COLOR=#0000ff]X0Y2[/color]
[COLOR=#0000ff]X1Y0[/color]
[COLOR=#0000ff]X1Y1[/color]
[COLOR=#0000ff]X1Y2[/color]
[COLOR=#0000ff]X2Y0[/color]
[COLOR=#0000ff]X2Y1[/color]
[COLOR=#0000ff]X2Y2[/color]
[COLOR=#0000ff]X15Y0[/color]
[COLOR=#0000ff]X15Y1[/color]
[COLOR=#0000ff]X15Y2[/color]
Output:
Code:
c:\Users\Roman\Work>perl stackdump.pl
X0Y0 X0Y1 X0Y2
X1Y0 X1Y1 X1Y2
X2Y0 X2Y1 X2Y2
X15Y0 X15Y1 X15Y2
 
Good grief! I had a go at this and ended up with about 50 lines of nonsense :(

Many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top