470,636 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Read coordinates

Hi,

I am new to Perl (as a matter of fact, I started learning it a couple of
days ago; I have some previous programming experience though). When
looking for the ideal languages to use for a particular project, I came
across perl, and starting writing the script.

I need to read a text containing some coordinates:
248,156
322,156
326,368
248,368

326,194
950,194
960,360
326,364

....

I need to process these coordinates and find the center of gravity of the
structure.

For now I have this code.

#! /usr/bin/env perl

open(HELL,"plc") or die "Can't open plc: $!";
my @x;
my @y;

#first read the file
#put the coordinates in special variables

while (<HELL>) { # assigns each line in turn to $_
$_ =~ /(\d+),(\d+)/;

for($i=0; $i<=3;$i++) {
@x[$i] = [ $1 ] ;
@y[$i] = [ $2 ];

print $x[1]; }

# print "the x is $1 \n";
# print "the y is $2 \n";
# print "Just read in this line: $_";

# do the necessary calculations
# [...]
}
I thought about putting each coordinate in an array of x and y values, and
then finding their arithmetic means.
However I do not know how to augment the arrays size at each iteration of
while. The size of the array cannot be changed can it?
Help would be greatly appreciated!

Thanks

BB
Jul 19 '05 #1
3 2252
Berk Birand wrote:
[...]
However I do not know how to augment the arrays size at each
iteration of while. The size of the array cannot be changed can it?


I think you are looking for the push() function, see "perldoc -f push"

jue
Jul 19 '05 #2
On Sun, 07 Mar 2004 00:25:49 +0000, Jürgen Exner wrote:
I think you are looking for the push() function, see "perldoc -f push"


Yes, that solved the problem quite well.
Thanks Jürgen.
Jul 19 '05 #3
In article <41******************************@news.teranews.co m>, Berk
Birand <gr************@NOSPAMyahoo.com> wrote:
Hi,

I am new to Perl (as a matter of fact, I started learning it a couple of
days ago; I have some previous programming experience though). When
looking for the ideal languages to use for a particular project, I came
across perl, and starting writing the script.

I need to read a text containing some coordinates:
248,156
322,156
326,368
248,368

326,194
950,194
960,360
326,364

...

I need to process these coordinates and find the center of gravity of the
structure.

For now I have this code.

#! /usr/bin/env perl

You should let Perl help you while you are developing code:

use strict;
use warnings;
open(HELL,"plc") or die "Can't open plc: $!";
my @x;
my @y;

#first read the file
#put the coordinates in special variables

my $i = 0; #(see below)
while (<HELL>) { # assigns each line in turn to $_
$_ =~ /(\d+),(\d+)/;
Always check to see if the match worked before using the results:

if( /(\d+), (\d+)/ ) {
for($i=0; $i<=3;$i++) {
Why are you assigning the extracted coordinates 4 times?
@x[$i] = [ $1 ] ;
This is assigning a reference to an array of one element ( [ $1 ] ) to
an array slice of one element ( @x[$i] ). This is not likely to be what
you want. @y[$i] = [ $2 ];
Maybe you want just:

$x[$i] = $1;
$y[$i] = $2;
$i++;

where you refer to the n'th element of @x as $x[n]. If so, then you
need the declaration of $i above, before and outside the loop.

print $x[1]; }

# print "the x is $1 \n";
# print "the y is $2 \n";
# print "Just read in this line: $_";

# do the necessary calculations
# [...]
}
Now you can do a calculation with the contents of @x and @y:

for my $i ( 0 .. $#x ) {
print "$x[$i], $y[$i]\n";
}
I thought about putting each coordinate in an array of x and y values, and
then finding their arithmetic means.
However I do not know how to augment the arrays size at each iteration of
while. The size of the array cannot be changed can it?
Help would be greatly appreciated!

Thanks

BB

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Oldchatterman | last post: by
8 posts views Thread by Stacey | last post: by
1 post views Thread by Cainnech | last post: by
reply views Thread by mingke | last post: by
???
reply views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.