Perl Array help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Array help
# 15  
Old 05-25-2012
uname -a
Code:
wyncc 2.6.34-12-desktop #1 SMP PREEMP 2010-06-29 02:39:08 +0200 x86_64 x86_64 x86_64 GNU/Linux

echo $SHELL
Code:
/bin/tcsh

and i got it to work.

i wast aware that the inputs where to be entered right after the test.pl

if you don't mind i would just kind of like to clarify some of the code to get a better understand of what each line is doing.

This segment is searching through the "input" file for the data between *NODE and * correct? and if it finds *NODE it wants all the data on the next line until the data just above *?
Code:
while (<COORD>) {
    chomp;
    if (/^\*NODE/ .. /^\*\*/) {
        if (/^\*NODE/) { next }
        if (/^\*\*/)   { last }


as for the next two lines i have absolutely no idea what they are doing.
Code:
@nos = split /\s+/;
        @nos = map { s/[.,]+$//; $_ } @nos;

Thanks!
# 16  
Old 05-25-2012
Quote:
Originally Posted by dets34
This segment is searching through the "input" file for the data between *NODE and * correct? and if it finds *NODE it wants all the data on the next line until the data just above *?
Code:
while (<COORD>) {
    chomp;
    if (/^\*NODE/ .. /^\*\*/) {
        if (/^\*NODE/) { next }
        if (/^\*\*/)   { last }

Yes, you're right.

Quote:
Originally Posted by dets34
as for the next two lines i have absolutely no idea what they are doing.
Code:
@nos = split /\s+/;
        @nos = map { s/[.,]+$//; $_ } @nos;

Thanks!
@nos = split /\s+/; => Split the line using whitespaces as delimiter and store the elements in array @nos

@nos = map { s/[.,]+$//; $_ } @nos => s/// is similar to sed's substitution. This is the general format: s/<pattern>/<replacement>/modifiers. So string matched by <pattern> is substituted by <replacement>. What we're doing here is, take each element of array @nos and delete last characters if they're a '.' or ',' (or rather substitute . or , by nothing). After this task take the elements and store them back in array @nos
# 17  
Old 05-25-2012
Next question:

This line is checking to see if near_dist is defined and if it's defined it sets near_dist = dist
Code:
if (!defined($near_dist)) {
            $near_dist = $dist;
        }


as for this next line im not to sure whats happening
Code:
elsif ($dist <= $near_dist) {
            $near_dist = $dist;
            @near_node = @nos;

---------- Post updated at 12:48 PM ---------- Previous update was at 12:25 PM ----------

also when i enter a negative value for a node position it gives me the incorrect node that is closest. Is there away to allow negative positions???????

---------- Post updated at 01:10 PM ---------- Previous update was at 12:48 PM ----------

im also getting
Use of uninitialized value in subtraction (-) for i believe every line data
# 18  
Old 05-29-2012
here is the data containing the negative coordinates.
# 19  
Old 05-29-2012
The format of the file attached in post #18 has changed. Your original requirement was to loop through the data co-ordinates which are in between "*NODE" and "**". And this data in between had few white spaces at the beginning of each line.

Since the data format provided now has changed, the program is not going into the if-condition block inside the while loop.

Try with this exact data sample (this is similar to your original requirement):
Code:
Random Text
Random Text
Random Text
*NODE
    70000000,        2536.143099644908,       -228.1225513919157,                 776.3626
    70000001,        2537.780882406166,      -224.58895334736727,                 776.3626
    70000002,                 2532.735,                  -224.66,                 776.3626
    70000003,                 2532.735,                 -227.938,                 776.3626
    70000004,       2511.7897270833937,                 -224.514,                 776.3626
    70000005,                 2506.435,                 -224.514,                 776.3626
    70000006,        2506.441591494802,      -227.63505426366342,                 776.3626
    70000007,        2512.065925587347,      -227.04047758786248,                 776.3626
    70000008,       2506.7046700485685,      -231.10340020092286,                 776.3626
    70000009,       2512.6415277961746,      -229.46730220429106,                 776.3626
    70000010,        2520.966873352281,         -238.49470282806,                 776.3626
**
Random Text
Random Text
Random Text

Code:
[user@host ~]# ./test.pl 2532 -228 776
Nearest node: 70000003 2532.735 -227.938 776.3626
Distance: 0.821917124776105
[user@host ~]#

Also, I've made a small modification in the code after line #24 (added line "@near_node = @nos" as line #25. This will help to display the node data incase the match is obtained in the very first line).

Last edited by balajesuri; 05-29-2012 at 09:59 AM..
# 20  
Old 05-29-2012
im still pulling data from the *NODE to **. I had the code put the data into a .txt file just for quick reference.
# 21  
Old 05-29-2012
Are you able to run the program successfully for data sample in post #19?

I see that the file format in post #18 is Macintosh. If you've transferred this file (in mac format) to unix or windows, then the perl program would not be able to process the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl array

Hi. I have 2 arrays, @result and @comment. Result contains 1 2 3 and comment contains a b c my code is below my @multi = (@result, @comment); foreach (@multi) { print array; } For this I am getting result 1 2 (2 Replies)
Discussion started by: sauravrout
2 Replies

2. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

4. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

5. Shell Programming and Scripting

perl array help !

I have a list of items in array say @array1=(apple=2,orange=3,peas=15,spinach=50); Another array has values say @array2=(apple,orange,plums,pineapple); I need to display a list if @array2 values is present in @array1 .else I need to print of the form(The final o/p may look like this.) OUTPUT... (5 Replies)
Discussion started by: gameboy87
5 Replies

6. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

how to get last value in an array in perl

Hi, I have a set of file names say: file1 file2 file3 .. filen I may just do a ls -l or however it works to read this list into an array... I need to pick the last file name that is "filen" from the list... your help is greatly appreciated.....hope im clear on my question... (7 Replies)
Discussion started by: meghana
7 Replies
Login or Register to Ask a Question