
mamat
Novice
Dec 13, 2009, 4:10 AM
Post #1 of 14
(1147 views)
|
|
Problem in processing multiple files
|
Can't Post
|
|
Hi all, My objective is to find the distance a particular node (e.g. Node 8) has travelled, and to do this I've done this in parts but when I combine these pieces together it does not work. My input data is as follows:
Node 8 (847.24, 357.59, 0.00). Node 17 (570.01, 963.66, 0.00). Node 20 (1367.53, 987.66, 0.00). Node 24 (1034.31, 1359.30, 0.00). Node 4 (1109.00, 242.11, 0.00). Node 8 (847.40, 358.58, 0.00). I use this script to extract just the co-ordinates of similiar nodes and direct them in corresponding files. Note that this creates many files.
#!/bin/bash file="./file" echo -e "Hi, please type the file name: \c " read word for ((i=1; i<=25; i++)); do grep -w "Node \<$i\>" $word | tr -d "()" | awk '{print $3, $4}' > node_$i.txt; done With the support of folks in this forum, I've have this script that calculates the distance as follows:#!/use/bin/perl use strict; use warnings; #no warning 'uninitialized'; use Data::Dumper; my @points = (); my $total = 0; open(IN, "some.txt") or die "$!"; while (my $line = <IN>) { chomp($line); my @array = (split (/\s+/, $line)); #print "@array\n"; push @points, [ @array ]; } close(IN); print '@points : ', Dumper \@points; for my $i1 ( 0 .. $#points -1 ){ my ( $x1, $y1, $z1 ) = @{ $points[$i1] }; my ( $x2, $y2, $z2 ) = @{ $points[$i1 + 1 ] }; my $dist = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2 + ($z2-$z1)**2 ); print "distance from ( $x1, $y1, $z1 ) to ( $x2, $y2, $z2 ) is $dist\n"; $total += $dist; } print "total distance is $total \n"; Note that I am using just a single file, some.txt to calculate the distance. While it works I have so many files (refer to the above script) and its not a good idea to manually type that many file names. So I've this code to read the file names and calculate the distance. This is the part that does not work:
#!/use/bin/perl use strict; use warnings; use Data::Dumper; my @points = (); my $total = 0; #http://www.daniweb.com/forums/thread75225.html opendir (DIR, "/media/KINGSTON/test/") or die "$!"; my @files = grep {/node_.*?\.txt/} readdir DIR; close DIR; foreach my $file (@files) { open(FH,"/media/KINGSTON/test/$file") or die "$!"; while (my $line = <FH>){ #read file line by line here chomp($line); my @array = (split (/\s+/, $line)); #print "@array\n"; push @points, [ @array ]; print '@points : ', Dumper \@points; for my $i1 ( 0 .. $#points -1 ){ my ( $x1, $y1 ) = @{ $points[$i1] }; my ( $x2, $y2 ) = @{ $points[$i1 + 1 ] }; my $dist = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2 ); print "distance from ( $x1, $y1 ) to ( $x2, $y2 ) is $dist\n"; $total += $dist; } } close(FH); } #open(IN, "some.dat") or die "$!"; #while (my $line = <IN>) { # chomp($line); # my @array = (split (/\s+/, $line)); # #print "@array\n"; # push @points, [ @array ]; #} #close(IN); #print "total distance is $total \n"; What is the mistake here so that I can calculate the distance for multiple files in one go? I am trying to get the total distance for co-ordinates contained files of format node*.txt. For e.g. for node1.txt, the distance is x and for node2.txt its y, and so on. Best Regards, Mamat.
|