473,480 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Assinging values to an array - perl beginner

1 New Member
hi,
I have a program to calculate the distance. its like this:
Expand|Select|Wrap|Line Numbers
  1. open(IN, "/path/outModified.pl") or die "$!";
  2.  
  3. while (my $line = <IN>) {
  4. chomp($line);
  5. my @array = (split (/\s+/, $line))[6, 7, 8];
  6. # print "@array\n";
  7. push @points, [ @array ];
  8. }
  9.  
  10. close(IN);
  11.  
  12. print '@points : ', Dumper \@points;
  13.  
  14. open my $out_file, '>', "/path/dist_modified.pl" or die "cannot open: $!\n";
  15.  
  16. for my $i1 ( 0 .. $#points -1 ){
  17. my ( $x1, $y1, $z1 ) = @{ $points[$i1] };
  18. for my $i2 ( 1 .. $#points){
  19. my ( $x2, $y2, $z2 ) = @{ $points[$i2] };
  20. my $dist = sqrt(($x2 - $x1)**2 + ($y2 - $y1)**2 + ($z2 - $z1)**2);
  21. print $out_file "153L.pdb: the distance between CA $i1 and CA $i2 = $dist\n";
  22. }
  23. }
  24. close $out_file or die "cannot close file: $!\n";
  25.  
And the distances($dist) looks like this

6.324 32.707 50.379
5.197 32.618 46.826
4.020 36.132 46.259
7.131 38.210 45.919
6.719 38.935 42.270
2.986 39.221 41.892
-0.269 37.184 41.565
-1.140 35.549 38.341
-4.817 35.710 39.211
-4.704 39.489 39.013

my question is, i have to compare these $dist values with another value in another file (DBins.pl)which has the data:

col1 col2
1 2.69
2 2.97032
3 3.25064
4 3.53096
5 3.81128
6 4.0916
7 4.37192
8 ....... so on.....
.
.
170 50.98
171 51.086....so on

I have to match the value of $dist with the column 2(col2) with its approximate value and retrieve the corresponding column1(col1)value? I have to match the value of $dist to the second column value(maximum is upto 50. any value of $dist greater than 50 should stop at the value(170 50.98)and retireve the value 170 for $dist compared with 50.98 or to its nearest high value. I dont know how to write this in perl. Can you help...The first column is the index value and second column is the energy value, so the energy value shud be compared to the $dist and the index value shud be printed in output.

To make it clear, if the value of $dist = 2.84, then the program should search for second row in the file [2 2.97032] and print only 2. that is the first column.it this possible. I think i have to use regular expressions?? or pattern matching??

pls help I am a beginner in perl.

I have written the code like this after calculating the $dist:
Expand|Select|Wrap|Line Numbers
  1. open(DIST,"path/DBins.pl") or die "$!";
  2. while(my $lines = <DIST>){
  3. chomp($lines);
  4. my @point = (split (/\s+/, $lines))[1,2];
  5.  
i dnt know how to assing values to the columns which i have split. Please help


Thank you in advance
Oct 7 '08 #1
1 2382
nithinpes
410 Recognized Expert Contributor
You may use this approach:
Expand|Select|Wrap|Line Numbers
  1. my %energy;
  2. open(DIST,"DBins.pl") or die "$!"; 
  3. while(my $lines = <DIST>){ 
  4. chomp($lines); 
  5. my ($in,$val) = (split (/\s+/, $lines)); # get the index and energy value
  6. $energy{$in} = $val;
  7. }
  8. close DIST;
  9. my %diff = map { $_ => abs($dist-$energy{$_})} (keys %energy);
  10. #get the key for smallest difference
  11. my $res=(sort {$diff{$a} <=>$diff{$b} } keys %diff) [0];  
  12. print "$res"; # print the index
  13.  
  14.  
Oct 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6730
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
1
4197
by: Reginald Carlier | last post by:
Hi, I'm programming a game and one of the variables is a two dimensional array in wich I store the names of the players. So far so good; with cin.get I ask the names of the players and...
9
4500
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
8
18572
by: nescio | last post by:
hello, i have an array and i don't know the content of it, but i want only unique values. in php there is a function to do this, but how must i do this in javascript? i have tried a lot and...
4
12781
by: Samuel Hardman | last post by:
Hello, I am trying to write a perl script to parse a string into an array. The string has the fields separated by tabs. So what I want to do is read each field into a variable so I can process...
29
3108
by: Amer Neely | last post by:
I've got a dynamically built form with checkboxes for each element ( a list of file names in a directory). I need to grab only those checkboxes that are checked, so I can then delete those files. ...
4
3728
by: Chronictank | last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a...
11
3182
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side...
2
1636
by: virtualweb | last post by:
Hello guys: Im a perl beginner (self taught) and Im unable to get elements from an array. Here is my situation step by step: 1) A list of emails is submitted, (one email per line), through...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.