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

formating character lenght of a line in a txt file 3

Status
Not open for further replies.

invalid6363

Programmer
Mar 21, 2008
24
0
0
US
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.
 
Instead of all those substr calls, you should use unpack for your 150-length format:
Code:
my ( $name, $account, $phone, $status, $address ) = unpack( 'A50A10A14A3A73', $line );
 
Oh, ok. i get it. thanks franco but one minor problem. when i ran the code for format 3 the address field did not print to the outfile but the other fields did and in the correct order.

instead of printing to outfile:
23573456 Mary Beth Davis 000 (541) 555-4321 234 Bridge St. Abc, OR. 24356

it printed:
23573456 Mary Beth Davis 000 (541) 555-4321

i copied the code exactly as you wrote it, but i am unsure why it does not print the address field.
Code:
elsif($line =~ /\s\d{3}\s([A_Z]{2})?\d{8}$/){
      my$_=$line;
      my@line=split;
      $account=pop@line;
      $status=pop@line;
      for(my$i=1;$i<@line;$i++){
         if($line[$i]=~/^\(\d{3}\)$/){
            $phone=join' ',$line[$i..$i+1];
            $name=join' ',$line[0..$i-1];
            $address=join' ',$line[$i+2..-1];
            last;
         }
      }
      print OUTFILE "$account\t$name\t$status\t$phone\t$address\n";
  }
 
Try replacing the line [tt]$address=join' ',$line[$i+2..-1];[/tt] with [tt]$address=join' ',$line[$i+2..$#line];[/tt] (don't know, but possibly an issue of perl version?).
If it doesn't work add the line [tt]for($i=0;$i<@line;$i++){print"$i:'$line[$i]'\n"}[/tt] after the [tt]$status...[/tt] line, and post what it prints (for a single record).



Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
ok, i made the change from [i+2..-1] to [i+2..$#line] and it is now working correctly but isn't -1 and $#line stating the same thing, to go from i+2 to the end of the list?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top