#!/usr/bin/perl use strict; use warnings; my $alter = "3"; #assign a value to alter different than 0 or 1 which is used later ... my $openfile = "$ENV{SystemRoot}/win.ini"; { open (MYFILE, ">>$openfile")or die "Can't open file 1: $!\n"; #open fo +r write, append not read! open (TEST, "c:/windows/win.ini") or die "Cant open file $!\n"; while(<TEST>) { if ( $_ =~ /[ARTEC]/ ) { my $alter = 1; } else { my $alter = 0; } } } if ($alter == 1) { #do something; print ".."; } close(MYFILE); close(TEST);

snippet of win.ini
blabla [ARTEC] IniFile=f:\vaa\basis\vaa01.ini bla
It seems like the while block never gets executed, any help?
Thx in Advance
MH
Update, Changed it now works:
not using my to declare variables ...
use vars qw($change $alter); $alter = 0; sub readfile { while(<TEST>) { if ( $_ =~ /^\[ARTEC\]$/ ) { $alter = 1; exit print "found it exiting no need to change something"; }else { $change = 1; print "n "; } } return; } } if ($alter == 0 and $change == 1) { #do something ... print "changed\n"; }

In reply to search and replace pattern inside file not working as expected by matze77

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.