Hi all! I'm new to Perl and I'm trying to use it to renumber the entries of a pdb file (sample below)

ATOM 9 H5T THY C 1 107.274 35.359 -9.821 0.00 0.00 N1 H

ATOM 10 O5' THY C 1 107.686 36.230 -9.553 1.00 0.00 N1 O

ATOM 11 C5' THY C 1 108.813 35.973 -8.710 1.00 0.00 N1 C

ATOM 12 H5' THY C 1 109.513 35.493 -9.239 0.00 0.00 N1 H

ATOM 13 H5'' THY C 1 108.495 35.550 -7.861 0.00 0.00 N1 H

ATOM 14 N1 THY C 1 107.956 38.157 -5.232 1.00 0.00 N1 N

ATOM 15 C6 THY C 1 107.862 39.006 -4.149 1.00 0.00 N1 C

ATOM 16 H6 THY C 1 108.479 39.755 -3.910 0.00 0.00 N1 H

ATOM 17 C2 THY C 1 107.024 37.166 -5.449 1.00 0.00 N1 C

ATOM 18 O2 THY C 1 107.071 36.398 -6.397 1.00 0.00 N1 O

ATOM 19 N3 THY C 1 106.027 37.104 -4.506 1.00 0.00 N1 N

So I want to renumber the entries in the second column (next to ATOM), starting them all sequentially from 1. This is what I've written so far:
#/bin/perl/ use strict; use warnings; my $pdb; my $line; my @columns; open $pdb, '<', "structure.pdb" or die $!; #match string and select column my $string = "ATOM"; # match for ATOM my @nth_column; my $n = 1; # select 2nd column while ($line = <$pdb>) { if ($line =~ $string) { chomp $line; @columns = split /\s+/, $line; push @nth_column, $columns[$n]; #for testing: print "$columns[$n] \n"; } }

So I am printing the correct column but I'm having a bit of a brain fart because I can't figure out how to actually do the renumbering? Would I match for a digit using regex and then introduce a counter? Does that sound reasonable?

Thanks in advance!!


In reply to renumber entries in column? by aria

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, details, 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, summary, 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.