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:

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:

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.

More details & the errors: I am trying to read through multiple files with file name of the format node*.txt and calculate the distance based on the co-ordinates it contains. Each file represents a different node. The second code snippet posted above calculate correctly the distance for co-ordinates in just a *single* file. But I want to do the same for multiple files contained in a directory. I am trying to do this using the code below:

I am getting too many lines of this error: Argument "370.12," isn't numeric in subtraction (-) at calc-dist.pl line 26, <FH> line 1. I do not know what this error means or if this error hides some other problem. Appeciate any help!

Thank you very much for your help. I wonder how I would have figured it out. Plus, I had not thought about processing my input in Perl itself. I used tr because I was a bit familiar with that. Your split code solves the error.

But how do I stop reading once I reach node1.txt, compute the distance, print and move on to the next file, i.e. node2.txt? A problem I notice that all the co-ordinates in the file are stored in the same array and thus am unable to differentiate if the distance is for node 1 or node 5.

The distance is now incorrectly calculated irrespective of the node file names. And they are printed incrementally!

I want to have all the node1.txt co-ordinates read, calculate their distance, and then move to the next node. What do need to do to achieve this? If there is an example that is demonstrates this, please let me know. Thanks Again.

Given your code, I thought I could access the co-ordinates and compute the distance as earlier. But I can't.

What is the purpose of

Quote

foreach my $points ( @{ $nodes{$node} } )

The first foreach is used to print the values of the hash, but the second one?

While I could access co-ordinates via my ( $x1, $y1 ) = @{$points}; how do I iterate to the next co-ordinate ($x2, $x2) so that I can use the distance formulae? I used a for loop which does not the print the values:

Thank you for your time & the code. Below is the version of the code that does what I want. I was trying to understand what the second foreach loop does in the last posts, because I didn't know how to iterate through the hash like an array. And am still not clear on $_, $, #, @ combinations.

The pragma is preferred because it is lexically scoped and can be turned on/off as needed.

The -w switch is globally scoped and can not be disabled. Since it's global, it is applied to files loaded with 'use' or 'require' and if those scripts/modules are not warnings safe, you'll get warnings that could be difficult to track down and fix.

You need to use proper indentation. At first glance your foreach loops appeared to be inside the while block. The closing brace for a while block should be at the same level of indentation as the start of the while keyword and the next section of code (i.e., your foreach block) should be at that same level of indentation.

It's generally not necessary to initialize a var to 0 unless not doing so could generate a warning or error.

It's best practice to test/verify that required user supplied data was actually provided and take proper action if it wasn't.

Quote

I didn't know how to iterate through the hash like an array.

You don't. The data structure we're using here is a HoA (Hash of Arrays). So, the first foreach loop is looping over the hash keys where the associated values are arrays. The second foreach loop is looping over the array elements.

The code above has a logic error. The line, $total += $dist; is in the wrong place. The procedure is to calculate the distances between the co-ordinates of node 1, then speed and move on to Node 2.

But because of the above line, Node 1's total distance is also included in the Node 2's. As a result, Node n's distance is a huge number when in reality the distances of all the nodes should relatively the same. Because of this error in the distance, the speed is subsequently incorrect.

If I put the dist outside the foreach loop, I get the distance is always 0. Where do I put the $dist so that it calculates only Node 2's distance and not include any other Node's distance.