Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: newbie need help with a simple code

by ww (Bishop)
on Mar 05, 2013 at 03:14 UTC ( #1021738=note: print w/ replies, xml ) Need Help??


in reply to newbie need help with a simple code

You could use a regex, but as you see, that can get clumsy (it need not be quite this clumsy; this is babytalk for clarity). Also, it assumes that each line of data you show is actually "an element of an array" as stated, and is NOT itself an array from an AoA:

#!/usr/bin/perl use 5.016; use warnings; use strict; use Data::Dumper; # 1021730 my @arr = ("ATOM 1276 CB ASP B 63 52.957 58.788 67.683 1.00 44.59 C", "ATOM 1280 N GLY B 64 52.075 55.496 66.522 1.00 35.29 N", "ATOM 1281 CA GLY B 64 51.005 54.534 66.322 1.00 32.64 C", "ATOM 1282 C GLY B 64 49.693 55.103 65.854 1.00 30.87 C", "ATOM 1283 O GLY B 64 48.630 54.545 66.092 1.00 27.93 O", "ATOM 1284 N LYS B 65 49.739 56.255 65.189 1.00 31.10 N", "ATOM 1285 CA LYS B 65 48.527 56.912 64.720 1.00 33.91 C", "ATOM 1286 C LYS B 65 48.629 57.147 63.216 1.00 32.04 C", "ATOM 1287 O LYS B 65 49.675 57.571 62.721 1.00 31.87 O" #Col1 2 3 4 5 6 7 8 9 10 11 12 ); # So, let's say columns 3, 6, 7, 10 and 11 are the ones you want to se +lect: my ($selected, @selections); for my $line(@arr) { if ( $line =~ /ATOM\s # leading "ATOM" followed by a space -- +e.g. Column 1 \d{4}\s # 4 decimal digits followed by a space ( +FBAS) ([A-Z]{1,2})\s # CAPTURE to $1: One or Two UC letters F +BAS from Col 3 [A-Z]{3}\s # Three UC letters B\s # "B" -- constant in example data at Col + 5 (\d\d)\s # CAPTURE TO $2 exactly 2 digits from Co +l 6 (\d+\.\d+)\s # CAPTURE to $3: digit(s), decimal pt, d +igit(s) Col 7 \d+\.\d+\s # 1 or more digits, decimal pt, digits C +ol 8: \d+\.\d+\s # Col 9 \d+\.\d+\s # Col 10 (\d\d\.\d\d)\s # CAPTURE to $4 exactly 2 digits, decima +l, 2 digits Col 11 ([CNO]$)/x ) # CAPTURE TO $5: one UC "C", "N", or "O" + adjacent to EOL { $selected = $1 . " | " . $2 . " | " . $3 . " | " . $4. " +| " . $5; say $selected; # for DEMO and DEBUG only push @selections, $selected; } # .... and do whatever sleight-of-hand you need with @selections +... } say Dumper @selections;


Comment on Re: newbie need help with a simple code
Download Code
Re^2: newbie need help with a simple code
by perlmonk007 (Novice) on Mar 05, 2013 at 04:46 UTC
    thank you so much for the prompt reply, could you please tell me what this line does? $selected = $1 . " | " . $2 . " | " . $3 . " | " . $4. "
      This is using the regex captures to create a string. see perldoc perlre
        Im sorry, i meant are the regex automatically captured to the scalars, if not what do i need to do to capture them into $1, $2 etc
        also the space between the characters is variable, it is not just one space, the file is such, could you tell me a regex for that please
Re^2: newbie need help with a simple code
by perlmonk007 (Novice) on Mar 11, 2013 at 18:17 UTC

    help anyone????

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021738]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-09-30 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (364 votes), past polls