I am unsure as to what you want. But using the single line that you have give as input, here are several ways to extract the 2nd thing past the "LOCUS" name:
#!/usr/bin/perl -w use strict; my $in = "LOCUS AJ877264 704 bp DNA linea +r INV 15-APR-2005"; my $other_in = " LOCUS AJ877264 "; #=================== # this is a bit "tricky"...$locus is in a list context # if you don't do that then you get True/False value of # the match my ($locus) = ($in=~ m/LOCUS\s+(\w+)/); print "LOCUS=$locus\n"; #PRINTS: #LOCUS=AJ877264 #============== # this does the same thing with spaces in front of LOCUS # in other words, no change is needed... my ($other_locus) = ($other_in=~ m/LOCUS\s+(\w+)/); print "LOCUS=$other_locus\n"; #PRINTS: #LOCUS=AJ877264 #================= # if you want to guarantee that the LOCUS you match is # the first on on line preceded by some possible blanks, # my ($other_locus) = ($other_in=~ m/^\s*LOCUS\s+(\w+)/); # will do it ^ start at beginning of line, then zero or # blanks, then LOCUS, then one or more blanks then a # sequence of "word" characters [0-9A-Za-z_]+ #=================== # This is a "list slice". # split(/\s+/,$in) is put into a list context with () # and the second thing, index [1] is moved to lvalue my $locus_also = (split(/\s+/,$in))[1]; #note "my ($locus_also) =" is also completely fine. print "Another way: LOCUS=$locus_also\n"; #PRINTS: #Another way: LOCUS=AJ877264 #=============== # the split version won't work with spaces in front # of LOCUS as a "" list element will be created: ($locus_also) = (split(/\s+/,$other_in))[1]; print "Another way 2: LOCUS=$locus_also\n"; #PRINTS: #Another way 2: LOCUS=LOCUS ($locus_also) = (split(/\s+/,$other_in))[2]; print "Another way 2: LOCUS=$locus_also\n"; #PRINTS: #Another way 2: LOCUS=AJ877264 #================= #or remove leading spaces, for the split case $other_in =~ s/\s*//; ($locus_also) = (split(/\s+/,$other_in))[1]; print "Another way 2: LOCUS=$locus_also\n"; #PRINTS: #Another way: LOCUS=AJ877264

In reply to Re^4: Writing to file... by Marshall
in thread Writing to file... by perl_n00b

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.