Hi monks I am struck up in a pretty bad problem.I have a file by the name of manager.properties.Now I am writtin g a code which will make an entry into this file.Now the present code(which I am writting below) erases any previous data into the file.Now i want to enhance this code so that if a user enters any data then it is matched with the contents of the file and if its not there then it is appended to the file and if it exists then it should be updated.Help me out please...
sub setManagers() { my $getInputFlag = 1; my $writeFileFlag = 1; my $mCounter = 0; my $ans = &promptUser("Do you wish to configure SNMP Managers +on $var? ", "yes"); if ($ans =~ /^[n]o?/i) { $getInputFlag = 0; $writeFileFlag = 0; } while ($getInputFlag) { my $mNumber = $mCounter+1; my $address; my $valid = 0; my $firstAttempt = 1; while (!$valid) { if ($firstAttempt) { $address = &promptUser("Enter Manager $mNumber IP Address +", "127.0.0.1"); $firstAttempt = 0; } else { $address = &promptUser("ERROR: $address Invalid: Re-enter +Manager $mNumber IP Address ", "127.0.0.1"); } if ($address =~ /^(([3-9]\d?|[01]\d{0,2}|2\d?|2[0-4]\d|25[0- +5])\.){3}([3-9]\d?|[01]\d{0,2}|2\d?|2[0-4]\d|25[0-5])$/) { $valid = 1; } } my $port = &promptUser("Enter Manager $mNumber SNMP Po +rt ", "162"); my $version = &promptUser("Enter Manager $mNumber SNMP + Version ", "2"); my $community = &promptUser("Enter Manager $mNumber +SNMP Community String ", "public"); $managers[$mCounter]{address} = $address; $managers[$mCounter]{port} = $port; $managers[$mCounter]{version} = $version; $managers[$mCounter]{community} = $community; $mCounter++; my $finish = &promptUser("Manager configuration comple +te [y/n]? "); if ($finish =~ /^y(es)?$/i) { $getInputFlag = 0; } } # write manager.properties file $JBOSS_HOME/conf if ($writeFileFlag) { open(FILE, "> $var:$ENV{JBOSS_HOME}/conf/manager.proper +ties"); my $numOfManagers = scalar(@managers); print FILE "manager.total=$numOfManagers\n"; for (my $i=0; $i<$numOfManagers; $i++) { my $mNumber = $i+1; print FILE "\n"; print FILE "# SNMP Manager $mNumber Settings\n +"; print FILE "manager.$mNumber.address=$managers +[$i]{address}\n"; print FILE "manager.$mNumber.port=$managers[$i +]{port}\n"; print FILE "manager.$mNumber.version=$managers +[$i]{version}\n"; print FILE "manager.$mNumber.community=$manage +rs[$i]{community}\n"; print FILE "\n"; } $writeFileFlag = 0; close(FILE); } }

In reply to updating to a file by s_gaurav1091

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.