Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem to write in a file

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello,
i've script who write in a file values they are stored in a array.
If the file existe, he take the variables and values from the files.
If i had a new variable in my file, he create me a new file with the variable are stored in the existing file but i've not the new variable i've add in the file.
For example
if i've no existing file the script create this file
#Login
export Login=tonny

#PAss connexion
export Password=xxxx

if i modify the file and change the password from xxxx to tonnypass, if i run the script and change nothing i've this
#Login
export Login=tonny

#PAss connexion
export Password=tonnypass

but if i want to add a new value like this
#Login
export Login=tonny

#PAss connexion
export Password=tonnypass

#port
export port=28

and if i run my script for modify the login,it create me a new file but i lose the variable i've created, i've this
#Login
export Login=benny

#PAss connexion
export Password=tonnypass
i don't understand ??

This is my script
Code:
#!/usr/bin/perl -w



use strict;
use warnings;
use English;
use Getopt::Long;
use POSIX qw(strftime);





###############################################################################


my $re = 0;
my $des     = 0;

my $filename           = 'confus.txt';

my $infile             = $filename;
my $madate             = strftime( '_%Y%m%d%H%M%S', localtime );
my $exist_file = 'false';
my $var;
my $com;

###############################################################################

my @connexion = ( 

{ "NAME" => "Login",
  "DEFAULTVALUE" => "tonny",
   "VALUE" => "", 
   "DEFAULTDESC" => "Login",
      "DESC" => "",
      "AUTOVALIDATED" => "false"
      }, 
      
 
      
      {"NAME" => "Password",
        "DEFAULTVALUE" => "xxxx",
   "VALUE" => "", 
   "DEFAULTDESC" => "PAss connexion ",
      "DESC" => "",
      "AUTOVALIDATED" => "false"}, 

      

     
   
      
);         

###############################################################################

my %vari_name;
foreach (@connexion) {
    $vari_name{ $_->{'NAME'} } = $_;
}

################################################################################

sub check {
    my $var_value = shift;
    my $var_name  = shift;
    my $ancienne  = $var_value;
    my $new;

    if ($des) {
        print $vari_name{$var_name}{"DESC"} . " :\n";
    }

  
    if ( length($var_value) == 0 ) {

       
        do {
            print $var_name . " [" . $var_value . "] : ";
            $var_value = <stdin>;
            print "\n";
            chomp($var_value);

        } while ( length($var_value) == 0 );
    }
    else {


        print $var_name . " [" . $var_value . "] : ";
        $var_value = <stdin>;
        $new       = $var_value;

        if ( ( length($new) - 1 ) == 0 ) {
            $var_value = $ancienne;
        }
        print "\n";
        chomp($var_value);
    }
    return $var_value;
}

###############################################################################




my $result = GetOptions(
       
    "res" => \$re,     
    "des"     => \$des          
);

if ( $result == 0 ) { exit 1; }





if ( -f $infile ) {
    $exist_file = 'true';
  

    open DATA, $infile or die $!;
    foreach my $intext (<DATA>) {




if ($intext =~ m/^\s*#\w+/ and $intext !~ m/^\s*#-\s*/)
    {
    
    $com = $com."\n".$intext;
    
    chomp($com);
    }

       
            my ( $name, $value ) = split /\s*=\s*/, $intext;
            chomp($name);
            chomp($value);
            $name =~ s/^\s*export\s+(\w+)/$1/;

            
            $vari_name{$name}{"VALUE"} = $value;
            $vari_name{$name}{"DESC"} = $com; 
         
            $com="";
        }
    }


    rename( $infile, $infile . $madate );

  
#}




if ( $re or ( $exist_file eq "false" ) ) {
    foreach $var (@connexion) {
        $var->{"VALUE"} = $var->{"DEFAULTVALUE"};
        $var->{"DESC"} = $var->{"DEFAULTDESC"};
    }
}
foreach $var (@connexion) {
 
        $var->{"VALUE"} = &check( $var->{"VALUE"}, $var->{"NAME"} );

}


open( FILE, '>', $infile )
  or die("impossible open $filename ");

foreach $var (@connexion) {

  
        
        print FILE "#" . $var->{'DESC'} . "\n";
        

        print FILE "export $var->{'NAME'}=$var->{'VALUE'}\n\n";
   
}



close(FILE);

thanks
 
How did you add the new value
#port
export port=28
to the file confus.txt - per hand or in your script ?
 
I looked at your 'problem'.
You runned your script,
then you have added with an editor these 2 lines
#port
export port=28
to the file confus.txt and then you have runned your script again and you wondered, why the above lines are no more in your file.
The answer is: because your script opens your file for writing i.e., your file will be every time overwritten.

If you want add the third variable to your file, you need to define it in the data structure in your script, e.g.:
Code:
my @connexion = ( 
{ "NAME" => "Login",
  "DEFAULTVALUE" => "tonny",
  "VALUE" => "", 
  "DEFAULTDESC" => "Login",
  "DESC" => "",
  "AUTOVALIDATED" => "false"
}, 
{"NAME" => "Password",
 "DEFAULTVALUE" => "xxxx",
 "VALUE" => "", 
 "DEFAULTDESC" => "PAss connexion ",
 "DESC" => "",
 "AUTOVALIDATED" => "false"
},
{"NAME" => "Port",
 "DEFAULTVALUE" => "28",
 "VALUE" => "", 
 "DEFAULTDESC" => "port",
 "DESC" => "",
 "AUTOVALIDATED" => "false"},  
);
 
i've aborted this post
thanks you for your response
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top