Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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;

In reply to Re: newbie need help with a simple code by ww
in thread newbie need help with a simple code by perlmonk007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and a log crumbles through the grate...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (5)
    As of 2018-06-20 15:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (116 votes). Check out past polls.

      Notices?