Hello :) Consider this bit of code.
my $stri="abctest"; my @one=('a','b','c','d','e','f','g'); my $part; my $lone=@one; my $first_char=substr($stri,0,1); #print "\n$first_char"; my $second_char=substr($stri,1,1); #print "\n$second_char"; my $third_char=substr($stri,2,1); #print "\n$third_char"; if ($first_char eq 'a' || $first_char eq 'b'){ for(my $ne=0;$ne<$lone;$ne++){ if ($second_char eq $one[$ne]) { if ($third_char ne 'c' || $third_char ne 'd'){ $part=substr($stri,0,2); print "\nThe string in part:"; print"\n$part"; } } } } if ($first_char eq 'a' || $first_char eq 'b'){ for(my $ne=0;$ne<$lone;$ne++){ if ($second_char eq $one[$ne]){ if ($third_char eq 'c' || $third_char eq 'd'){ $part=substr($stri,0,3); print "\nThe string in part:"; print"\n$part"; } } } }
When we run the above code with $stri equal to "abctest" we should get a match in the second section of loops only
i.e. if the first char in the string is 'a' or 'b' , second char is any of the elements of @one, and third char is 'c' or 'd'.
The value of part should be 'abc'.

But there is a match in both the sections of loops, and this is the output I get:
The string in part: ab The string in part: abc
This comparison does not seem to be working:
if ($third_char ne 'c' || $third_char ne 'd'){ $part=substr($stri,0,2);
What is the mistake here?
Thanx :)

In reply to Loops and comparisons by perl_seeker

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.