invalid6363
Programmer
i need help on writing some code that will take each line of a file and determine if it contains the right length of characters for each line (150 max). Each line in the file contains 5 fields and has an assigned number of characters:
name (0-49)
address (50-59)
phone number (60-73)
status (74-76)
account (77-149)
if any of the fields contains less than the max number of characters, then the field should be right-padded with spaces.
i know that i can use the built-in length function to get the total, but not sure on how to check the individual fields.
i was thinking i would first split $line into in an array and then assign them variables.then check the length of each one of these and add spaces if needed using sprintf function.
if(length($line) < 150)
{
@line = split(/,+/,$line);
($name,$address,$phone,$status,$account) = @line;
if(lenght($name) < 50)
{
$pad_len = 50 - length($name);
$padded = sprintf outfile "%-*s", $pad_len, $name";
}
...repeat process for other fields
}
but looking at this, it seems that it will print each of the fields one at a time on a different line in the output file, instead of all on one line.
does anybody have a better way to do this or any suggestions on how to improve this code?
any suggestions would be greatly appreciated.
name (0-49)
address (50-59)
phone number (60-73)
status (74-76)
account (77-149)
if any of the fields contains less than the max number of characters, then the field should be right-padded with spaces.
i know that i can use the built-in length function to get the total, but not sure on how to check the individual fields.
i was thinking i would first split $line into in an array and then assign them variables.then check the length of each one of these and add spaces if needed using sprintf function.
if(length($line) < 150)
{
@line = split(/,+/,$line);
($name,$address,$phone,$status,$account) = @line;
if(lenght($name) < 50)
{
$pad_len = 50 - length($name);
$padded = sprintf outfile "%-*s", $pad_len, $name";
}
...repeat process for other fields
}
but looking at this, it seems that it will print each of the fields one at a time on a different line in the output file, instead of all on one line.
does anybody have a better way to do this or any suggestions on how to improve this code?
any suggestions would be greatly appreciated.