while going through input file with each line as below format with below script

--file--
server1 M.22 s.23 2 AB 192.16.1.1 0 42 BBS 50
server3 M.22 s.33 2 AB 192.16.1.2 0 42 BBS 50 MK
server3 M.49 s.49 3 BB 10.1.1.1 0 42 BS 50
and more............
Program runs fine but I am only trying to replace 10th field's 50 with something else and since data is random, sometimes field 3 has 50 or 150 on it and messing up the program.
Can someone please give me a pointer for me to write this better ?
I am guessing
if ($cou) { $orig =~ s/\b$itemY\b/$default/; print "$orig\n";
is just looking at whichever 50 that occurs first and doing the substituion.
Can someone please give me a pointer on this
while (<>) { chomp; my $orig = $_; if ($orig =~ /^#/) { print "$_\n"; next; } if ($orig =~ /^$/) { print "$_\n"; next; } if (/(\w+)\s+M\.\d+\s+(s\.\d+)\s+(\S+\s+){6}(\d+)(.+)?$/) { #server1 yahoo.22 s.50 2 ABCCD 192.168.1.1 0 42 BBS + 50 my $cou = 1; my $item = join('@', $2, $1); my $itemY = $4; for my $i_carriers (keys %s_carriers) { if ("$item" eq "$i_carriers") { my $itemX = $s_carriers{$item}; $orig =~ s/\b$itemY\b/$itemX/; print "$orig\n"; $cou = 0; } last if ! $cou; } if ($cou) { $orig =~ s/\b$itemY\b/$default/; print "$orig\n"; } } }

In reply to regex question by convenientstore

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.