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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array record element change....HELP!!!

Status
Not open for further replies.

networkguy

Technical User
Jun 12, 2001
3
0
0
US
I am a Perl newbie so be easy on me! I have an array of multiple records (user_header1.txt) which looks something like:

name; color; groups; auth_method; internal_password;
111-13-3009 ;Foreground; {fwadmins};undefined;dd;
(where the 1st line is a header and the records follow.)
I am trying to replace an internal_password field of the record and save the change to the same file. When I'm running the script below, it obviously overwrites the record with just name and password. How can I change that particular field while preserving the rest of the records???

open(USERS, "> user_header1.txt");
print USERS "name;\tinternal_password;\n";
print USERS "$uid;\t$new_passwd1;\n";
close (USERS);
 
I would do it this way:

1. in a while loop, read each line of the file
and use perl's "split" command to separate
the line into fields, something like this:

($name, $color, $groups, $auth_method, $internal_password, $foreground, $whatever) = split(/;\t/, $line);

2. for each line
* calculate the new password, and assign it to $internal_password
* print the line to a new output file, using each of the variables in the print

3. when all lines have been written to the new output file, "move" the new output file to the old output file - overwriting the old output file. I'd use module File::Copy since it makes moving and copying files very easy.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top