Read coordinates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Berk Birand

    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


  • Jürgen Exner

    #2
    Re: Read coordinates

    Berk Birand wrote:
    [...][color=blue]
    > 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?[/color]

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

    jue


    Comment

    • Berk Birand

      #3
      Re: Read coordinates

      On Sun, 07 Mar 2004 00:25:49 +0000, Jürgen Exner wrote:[color=blue]
      > I think you are looking for the push() function, see "perldoc -f push"[/color]

      Yes, that solved the problem quite well.
      Thanks Jürgen.

      Comment

      • Jim Gibson

        #4
        Re: Read coordinates

        In article <41ed1ab461951f 4a2a1d604f4a0ea 5d5@news.terane ws.com>, Berk
        Birand <graffitiNOSPAM @NOSPAMyahoo.co m> wrote:
        [color=blue]
        > 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
        >[/color]

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

        use strict;
        use warnings;
        [color=blue]
        > open(HELL,"plc" ) or die "Can't open plc: $!";
        > my @x;
        > my @y;
        >
        > #first read the file
        > #put the coordinates in special variables
        >[/color]

        my $i = 0; #(see below)
        [color=blue]
        > while (<HELL>) { # assigns each line in turn to $_
        > $_ =~ /(\d+),(\d+)/;[/color]

        Always check to see if the match worked before using the results:

        if( /(\d+), (\d+)/ ) {[color=blue]
        >
        > for($i=0; $i<=3;$i++) {[/color]

        Why are you assigning the extracted coordinates 4 times?
        [color=blue]
        > @x[$i] = [ $1 ] ;[/color]

        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.[color=blue]
        > @y[$i] = [ $2 ];[/color]

        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.
        [color=blue]
        >
        > 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
        > # [...]
        > }[/color]

        Now you can do a calculation with the contents of @x and @y:

        for my $i ( 0 .. $#x ) {
        print "$x[$i], $y[$i]\n";
        }
        [color=blue]
        > 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
        >
        >[/color]

        Comment

        Working...