Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

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

You have already been given the pieces you need, but they don't fit your hand and you haven't shown us what you have tried when you say "I still don't get it to work". The code below is a slightly more fully worked example using suggestions you have already been given:

use strict; use warnings; my $inputLines = <<LINES; TAAGAACAATAAGAACAA TAAGAACA.TAAGAACAA TAAG-ACA.TAAGAA_AA LINES open my $inFile, '<', \$inputLines or die "Can't open file: $!\n"; while (my $line = <$inFile>) { chomp $line; next if $line !~ /([^ACGT])/; print "In line $.: $line\n"; my $offset = 0; while ($line =~ /([^ACGT])/) { print "Found $1 at ", $-[1] + $offset, "\n"; substr $line, 0, $-[1] + 1, ''; $offset += $-[1] + 1; } }

Prints:

In line 2: TAAGAACA.TAAGAACAA Found . at 8 In line 3: TAAG-ACA.TAAGAA_AA Found - at 4 Found . at 8 Found _ at 15

This is a "Simple Self Contained Example". You can run the code without needing anything else. You should first copy this code (cut and paste is highly recommended) and check that it works yourself. Then play with it until you have some understanding of how it works. Then adapt it to you own needs.

There are some important things there. Note the use of strictures (use strict; use warnings;). Always use strictures in your code! The my $inputLines = <<LINES; and following lines create a variable initialised with multiple lines of text. That is used in open my $inFile, '<', \$inputLines or die "Can't open file: $!\n"; as a file. You can replace \$inputLines with a file name to open a file instead.

In while (my $line = <$inFile>) { you could instead use <STDIN> to read lines from the command line.

You are already using a regular expression so we assume you know something about those. If you don't, ask. The new bit is that $-[$n] gives the 0 based position (index) of the $n'th match.

substr is used to trim the line to the point of the matched character ready to find the next bad character.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re: Find element in array by GrandFather
in thread Find element in array by Sofie

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2020-08-05 11:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which rocket would you take to Mars?










      Results (35 votes). Check out past polls.

      Notices?