http://qs321.pair.com?node_id=11113031


in reply to Find element in array

#!/usr/bin/env perl # https://www.perlmonks.org/?node_id=11113020 use Data::Dumper; $Data::Dumper::Sortkeys=1; # make it easier +to find "things" in the "dumps" # Yes we'll take all the help we can get use strict; use warnings; # Read the data one line at a time while (<DATA>) { # Same as while +($_=<DATA>) { # And get rid of the $INPUT_LINE_SEPARATOR (\n); chomp; # Same as chomp( +$_); warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; # Let's see what + we have ... there shouldn't be a trailing \n (my $bad=$_)=~ tr/[ATCG]/ /; warn Data::Dumper->Dump([\$_],[qw(*_)]),' '; # So all the val +id ones are gone if ($bad !~ m{^\s+$}) { # Are there any +bad ones? print "line: $.\n" # Yes - so print + the offending line number print "$_\n"; # The offending +line print "$bad\n"; # The offending +character(s) in the line $bad=~ s{\w}{print 1+pos($bad),','}eg; # Look for a "ch +aracter" if you find one print its "location' print "\n"; }; }; __END__ TAAGAACAATAAGAACAA TAAGAACAATAAUAACAA TAYGAACAkTAAGAACzz

Yields

perl 3020a.pl 2> nul TAAGAACAATAAUAACAA U 13, TAYGAACAkTAAGAACzz Y k zz 3,9,17,18,

Replies are listed 'Best First'.
Re^2: Find element in array
by GrandFather (Sage) on Feb 17, 2020 at 03:35 UTC

    This reply would be better if it actually compiled. Line 19 (the first print in the if body) is missing a semi-colon - obviously not the code you ran to produce the given output!

    It would also be better to use a manifest variable instead of the default variable in the while statement so both the intent and scope are clearer. Bundling an assignment and operation on a variable into one line probably isn't best practice in an example for someone new to Perl ((my $bad=$_)=~ tr/[ATCG]/ /;).

    Do you really expect $bad=~ s{\w}{print 1+pos($bad),','}eg; to make sense to an entry level Perl user, even with the comment?

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

      mea culpa, mea culpa, mea maxima culpa!