wrapit Perl Code List

This is the Perl code for wrapit, a small formatting program that reads SAS input or SPSS data list statement and outputs them five to a line. This assumes you have Perl available and that it can be found by the "env" program. If neither of those conditions are met, you can still run the program as perl wrapit.pl (or perl wrapit5.pl).

The basic algorithm here is push the code statements onto an array, then loop through the array printing them out in the format desired. To get the code statements, some processing takes place on the data coming in. It's only removing leading and trailing blanks (for example, s/^\s+// for removing leading blanks). Thus, not too crazy for Perl.

If you want a different output format, just modify the printf command. Or modify the "for" statement if you want a different number of columns (i+=5). Since it currently does 5 columns the program was called wrapit5.

This utility is part of a collection of more text-processing tools.


#!/usr/bin/env perl

# Wrapit: Read input or data list statements (e.g. "VAR1 12-18 .4", one
# line at a time and output them in wrapped format, five to a line
#  Kent Nassen, 4/22/97 & 8/15/97

# Sample input:
#  V1 1-4
#  V2 5-8
#  V3 9-9
#  V4 10-13
#  V5 14-14
#  V6 15-16
#  V7 17-17
#  V8 18-19
#  V9 20-21

# Sample output:
#   V1 1-4                   V2 5-8                   V3 9-9                   
#   V4 10-13                 V5 14-14                 V6 15-16                 
#   V7 17-17                 V8 18-19                 V9 20-21

# Read the input statements into an array
while (<>) {           # read from file on commandline
  chop;                # drop line ends
  for ($_) { s/^\s+//; s/\s+$//; } # strip leading & trailing blanks
  push @elements, $_;  # build array
  }

# Print out the array elements in preferred order & format
   for ($i=0; $i<=$#elements; $i+=5) {  # we want a 5-wide listing
      printf("   %-15s%-15s%-15s%-15s%-s\n",${elements}[$i],${elements}[$i+1],
		${elements}[$i+2],${elements}[$i+3],${elements}[$i+4]); 
   }


Back to Kent's Perl Page
Last Modified: Tue Sep 9 22:01:06 EDT 2025