horizontal join of array elements

J

Jorge

Hi, it's me again -- Jorge

My input file is a list of x/y coordinates representing a geometry with
each vertex listed vertically on seperate lines. There are several
shapes in the list and each one always has a START delimiter pattern of
/initial/ and will always have a STOP delimiter of either /polygon/ or
/attribute/. The /terminal/ lines between the delims need to be
retained.

I need to gather each set of vertices into a horizontal string
seperated by whitespace.

So far I have managed to isolate the individual groups (after cleaning
them) and then I push the members of each group into an array with the
hopes of joining them on whitespace.

The program works (I think) until I apply the join command at which
time it still returns a vertical listing where I had expected a
horizontal string of all the elements.

I've been through perlfaq5/6 and other places and can not see what I
did wrong with this join command.

Any help is appreciated, Jorge.

Code begins here ....

open (FILE, "file.txt") or die "cant open file: $!";

my @arr;
my $arr;
my $clean_string;

while(<FILE>){
if(/initial|terminal/){
$clean_string = cleanser("$_");
push(@arr, "$clean_string");
}
elsif(/polygon|attribute/){
$clean_string = cleanser("$_");
push(@arr, "$clean_string");
}
$arr = join(" ", @arr);
print "$arr";
@arr = (); $arr = "";
}

close(FILE);

sub cleanser{
$_[0] =~ s/\$\$initial\(\[//g;
$_[0] =~ s/\$\$terminal\(\[//g;
$_[0] =~ s/\]\, \, \@nosnap \)\;//g;
$_[0] =~ s/\] \)\;//g; s/\]\)\;//g;
return $_[0];
}

file.txt begins here ...

$$initial([-0.086,0.062], , @nosnap ); # <- START
$$terminal([-0.052,0.062] );
$$terminal([-0.052,0.138] );
$$terminal([-0.061,0.138] );
------- snip ------
$$terminal([-0.061,-0.114] );
$$terminal([-0.052,-0.114] );
$$terminal([-0.052,-0.062] );
$$terminal([-0.086,-0.062] );
$$polygon( "POWER" ); #<- STOP
$$initial([-0.046,-0.022], , @nosnap ); #<- START
$$terminal([-0.012,-0.022] );
$$terminal([-0.012,-0.154] );
$$terminal([-0.021,-0.154] );
-------- snip --------
$$terminal([0.012,-0.154] );
$$terminal([0.012,-0.022] );
$$terminal([0.064,-0.022] );
$$terminal([0.064,0.022] );
$$attribute(...); #<- STOP
..
..
..
more of the same
 
A

A. Sinan Unur

The program works (I think) until I apply the join command at which
time it still returns a vertical listing where I had expected a
horizontal string of all the elements.

This is clear as mud. I hope you have considered using Data::Dumper to look
into your data structures and also run this script in the debugger to see
what is happening at each stage.
Code begins here ....

It is best to format your code so it can easily be read by other people.
open (FILE, "file.txt") or die "cant open file: $!";

my @arr;
my $arr;
my $clean_string;

You don't need $clean string in this scope.
while(<FILE>){
if(/initial|terminal/){
$clean_string = cleanser("$_");

Useless use of quotes.
push(@arr, "$clean_string");

Useless use of quotes.

Use capturing regular expressions to extract the part of the string that
you are interested in.
elsif(/polygon|attribute/){
$clean_string = cleanser("$_");

Useless use of quotes.
push(@arr, "$clean_string");

Useless use of quotes.

You are doing the exact same thing in the 'if' and 'else'.

Based on your verbal description, I came up with something. It would be
better if you could give an example of what output you expect from the data
you showed. This deals with only part of the data you posted because I am
not sure what to with 'attribute' etc.

#! perl

use strict;
use warnings;

my @x;
my @y;

my %polygons;

while(<DATA>) {
if( /^\$\$polygon\(\s*"(\w+)"\s*\);$/ ) {
$polygons{$1} = { x => [ @x ], y => [ @y ] };
@x = @y = ();
next;
}
if( /^\$\$initial\(\s*\[(.+)\]/
|| /^\$\$terminal\(\s*\[(.+)\]/) {
my ($x, $y) = split /,/, $1;
push @x, $x;
push @y, $y;
next;
}
}

for my $p (values %polygons) {
print 'x => ', join(' ', @{ $p->{x} }), "\n";
print 'y => ', join(' ', @{ $p->{y} }), "\n";
}


__DATA__
$$initial([-0.086,0.062], , @nosnap );
$$terminal([-0.052,0.062] );
$$terminal([-0.052,0.138] );
$$terminal([-0.061,0.138] );
$$terminal([-0.061,-0.114] );
$$terminal([-0.052,-0.114] );
$$terminal([-0.052,-0.062] );
$$terminal([-0.086,-0.062] );
$$polygon( "POWER" );
__END__

D:\Home>perl t6.pl
x => -0.086 -0.052 -0.052 -0.061 -0.061 -0.052 -0.052 -0.086
y => 0.062 0.062 0.138 0.138 -0.114 -0.114 -0.062 -0.062
 
J

Jorge

For starters I had the post formatted when I pasted it into the dialog
-- maybe I need to see if my editor is not working correctly on
cut-n-paste.

I expect the output to look like this for each group ...

-0.052,0.062 -0.052,0.138 -0.061,0.138 -0.061,-0.114 -0.052,-0.114
-0.052,-0.062 -0.086,-0.062 polygon( "POWER

The last token can also be ATTRIBUTE if that is the case
I apologize for the confusion

Jorge
 
A

A. Sinan Unur

I expect the output to look like this for each group ...

-0.052,0.062 -0.052,0.138 -0.061,0.138 -0.061,-0.114 -0.052,-0.114
-0.052,-0.062 -0.086,-0.062 polygon( "POWER

Please quote some context. From your original post:

$$initial([-0.086,0.062], , @nosnap ); # <- START
$$terminal([-0.052,0.062] );
$$terminal([-0.052,0.138] );
$$terminal([-0.061,0.138] );
------- snip ------
$$terminal([-0.061,-0.114] );
$$terminal([-0.052,-0.114] );
$$terminal([-0.052,-0.062] );
$$terminal([-0.086,-0.062] );
$$polygon( "POWER" );

Or you now saying that you actually want to disregard the initial points?

Anyway, it should be trivial to modify the code I posted to do this.

Sinan.
 
J

Jorge

Sinan

You have given me more than enough to work with and I thank you for
taking the time to do so.

Jorge
 

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,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top