Read coordinates

B

Berk Birand

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
 
J

Jürgen Exner

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
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top