473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ 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)va lue? 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 2392
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
6757
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 clearly be the most efficient way to do it, but it only works on a copy of the original array and not the original (which is counter intuitive in my estimation). Using each doesn't work consistently either. Not only that, it's unduly complex for...
1
4210
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 everything works fine. Now I want the player also to be able to play against the computer and I don't want him to fill in the name "computer" interactively; I want to set the second element of the two-dimensional array to computer. When i do player;
9
4520
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 purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions. I know that the web offers a lot of resources on Python/Perl differences. But I couldn't find a...
8
18596
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 it is driving me nuts
4
12798
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 the data further. what I have is something like this; open($rpt, $file); while (<rpt>)
29
3154
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. Does each checkbox name have to be unique? I was hoping to just group them under one name, and select from that array. PHP is not my native language - I'm coming at this from Perl, so bear with me, things are a little different in that country.
4
3747
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 language to connect to a SQL database in a Unix envirment as well as script parsing so there is more than likely a ton of things i could have done a better way. I have been at this for 3 days now and cant for the life of me work out why the code is...
11
3202
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 programming since I am in the field of web design, development and multimedia. Here are some tips and need some help how to program: The way I learn is that I need an detailed explanation why and how we need use certain functions in PERL: I...
2
1665
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 the texarea of a form named "Email_List" to a script that will check email sintax, etc List follows: (some emails have wrong syntax on purpose). ben_wade@gmail.com
0
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6081
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4713
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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 we have to send another system
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.