CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...: Re: [marky9074] Extract from text file, basic trig functions, then graph: Edit Log



Chris Charley
User

Oct 18, 2017, 1:01 PM


Views: 37037
Re: [marky9074] Extract from text file, basic trig functions, then graph

I can't comment on the graphing you want, but can offer a solution to the data extraction.

This solution captures all the lines beginning with 'R', (noticed in your file that there were lines not beginning with R towards the end of the file - I just exited the file when I encountered them).

I found the amount of memory to store the data and it was 1_897_456 bytes, not too large I think.

I used unpack to extract the data and then ran the program which, at the end, prints out the sample ID's and their east north coordinates.

Code
#!/usr/bin/perl 
use strict;
use warnings;
# use Devel::Size 'total_size';

my $format = "x a4 x a8 a9 x4 a4 x a8 a9 x4 a4 x a8 a9";
# ID E N ID E N ID E N

my @data;

# reads from 'file2'
while (<>) {
last unless /^R/;
my @temp = unpack $format;

for my $i (0 .. $#temp) {
if ($i % 3 == 0) {
$data[ $temp[$i] ] = [ @temp[ $i+1, $i+2 ] ];
}
}
}

printf "%4s %10s %s\n", qw/ID East North/;

for my $id (1, 564, 565, 1128, 1129) {
printf "%4s %10s %s\n", $id, $data[$id][0], $data[$id][1];
}

#print total_size \@data; # prints '1_897_456'


The output from this program was -


Code
 
C:\Old_Data\perlp>perl test2.pl file2
ID East North
1 453448.0 7998331.8
564 447738.7 8002432.1
565 453478.2 7998372.2
1128 447767.3 8002471.8
1129 453507.3 7998412.9

C:\Old_Data\perlp>


Note that the way to access these east north coordinates can be seen in the print routine (at the bottom of the program).


Code
$data[$id][0], $data[$id][1];


I used the modulus (%) operator to determine the index of the id in the '@temp' array in the loop. I also used an array 'slice' to assign the east north values for the id.

'@temp[ $i+1, $i+2 ]'


(This post was edited by Chris Charley on Oct 18, 2017, 3:14 PM)


Edit Log:
Post edited by Chris Charley (User) on Oct 18, 2017, 3:11 PM
Post edited by Chris Charley (User) on Oct 18, 2017, 3:14 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives