Extra spaces in output and concatenating columns 
Author Message
 Extra spaces in output and concatenating columns

Hey, folks-
        I've started getting interested in Perl because an awful lot of
my job as an IS auditor consists of plowing through text files generated
on many different systems, extracting some piece of information and then
manipulating that info (sounds like a pretty ideal job for Perl, no?).

        One of the more common tasks I have is to reconcile the user
accounts on systems with the human resources list of current employees.
Usually I end up with a text file generated from a redirection of the
networks output and a DBase file from HR.  Then, using an awkward
combination of Monarch (which is a great program, but somewhat
inflexible), joins in Access, and Excel, I set about coming up with
accounts belonging to ex-employees.

        With three nights of reading the Camel Book and a
vague notion of C, I've hacked out something which sometimes works based
on the Common Tasks section:

----

open (UID, "$ARGV[0]");
open (HR, "$ARGV[1]");

local (%mark);


close (UID);
close (HR);
----

(note:  I'm using bigperl for MS-DOS, so if the argument vector
references seem strange it's because I haven't taken the time
to figure out how to invoke perl scripts other than:
        perl <perlprog> <infile1> <infile2>...
)

OK, I'm no programmer.  Ignoring the lack of any error catching, can
someone tell me why the second and successive lines of output are
preceeded by a space?

Second, these lists frequently come my way in multi-column form.  I was
able to come up with the following program to create a single column
out of three, but it requires me to manually count and enter the
column sizes.  Surely there must be a better way:

----
while (<>) {
        chop $_;



Quote:
}


----

These columns are separated by spaces or tabs, and I'm sure that there
exists a better method based on regular expressions.  Any suggestions?

Mark.



Mon, 27 Jan 1997 22:00:49 GMT  
 Extra spaces in output and concatenating columns


Quote:
>Hey, folks-
>        I've started getting interested in Perl because an awful lot of
>my job as an IS auditor consists of plowing through text files generated
>on many different systems, extracting some piece of information and then
>manipulating that info (sounds like a pretty ideal job for Perl, no?).

>        One of the more common tasks I have is to reconcile the user
>accounts on systems with the human resources list of current employees.
>Usually I end up with a text file generated from a redirection of the
>networks output and a DBase file from HR.  Then, using an awkward
>combination of Monarch (which is a great program, but somewhat
>inflexible), joins in Access, and Excel, I set about coming up with
>accounts belonging to ex-employees.

>        With three nights of reading the Camel Book and a
>vague notion of C, I've hacked out something which sometimes works based
>on the Common Tasks section:

>----

>open (UID, "$ARGV[0]");
>open (HR, "$ARGV[1]");



>local (%mark);



>close (UID);
>close (HR);
>----

>(note:  I'm using bigperl for MS-DOS, so if the argument vector
>references seem strange it's because I haven't taken the time
>to figure out how to invoke perl scripts other than:
>        perl <perlprog> <infile1> <infile2>...
>)

>OK, I'm no programmer.  Ignoring the lack of any error catching, can
>someone tell me why the second and successive lines of output are
>preceeded by a space?


print's output string.  When you do that, the effect is the same

$" holds the separator that goes between list elements.  If you set
$" = '', you will probably see what you expect.

- Show quoted text -

Quote:
>Second, these lists frequently come my way in multi-column form.  I was
>able to come up with the following program to create a single column
>out of three, but it requires me to manually count and enter the
>column sizes.  Surely there must be a better way:

>----
>while (<>) {
>        chop $_;



>}

>----

>These columns are separated by spaces or tabs, and I'm sure that there
>exists a better method based on regular expressions.  Any suggestions?


user names and strings with a single newline in turns.  You'd probably
be better off when you deal with the newlines on output when you need
them.

That said, the following snippet does much the same as your program
for multi column files of user names, as well as for more erratic
ones:

while ( <> ) {

Quote:
}

$" = "\n";

The call to split( " " ) is the interesting part.  It splits the string
$_ (by default) on whitespace, ignoring leading white space.  So it
gives you a list of the user names on the current line, or an empty

Anno



Mon, 27 Jan 1997 23:59:43 GMT  
 Extra spaces in output and concatenating columns


Quote:
>Second, these lists frequently come my way in multi-column form.  I was
>able to come up with the following program to create a single column
>out of three, but it requires me to manually count and enter the

This reminds me: I recently had need to concatenate two files columnwise; that
is, place the text in two files in adjacent columns separated by, oh, say a
tab.  Here's what I came up with:

=====
#!/usr/local/bin/perl
#
# Concatenate two files in two separate columns

open(FH1, shift);
open(FH2, shift);
while ($line1 = <FH1>, $line2 = <FH2>, defined($line1) || defined($line2)) {
    chop($line1);
    chop($line2);
    print "$line1\t$line2\n";

Quote:
}

=====

My question is:  is there a perl-ism for reading in multiple files
"concurrently"?  Say, if I wanted to generalize the above script for n files.
I realize it's pretty easy to extend the above example, but I wonder if there's
something as elegant as the "<>" construction, which reads in multiple files
consecutively.  (Plus, say the files are too big to read into memory in their
entirety.)

Top

PS  Yeah, I know, the second "chop" in my script is redundant.  Oh well.
--
W. Top Changwatchai



Sat, 01 Feb 1997 23:21:11 GMT  
 Extra spaces in output and concatenating columns
Here is a script I wrote to approximate the unix "paste"
command on the NeXT (which uses paste for the pasteboard
instead).  No claims of elegance or originality, but it works
for me.

- David C. Lambert

-------------------------------> CUT HERE <----------------------------
#!/usr/local/bin/perl
#
#   PPASTE
#   Perl script to concatenate files columnwise.
#
#   Author: David C. Lambert

#
#   Usage: ppaste [-s separator_string] file1 file2 ...
#
do "getopts.pl";

# define separator as space or from cmd line option
$sep = " ";
do Getopts("s:");
$sep = $opt_s if (defined($opt_s));

# read each filename and open to distinct filehandle(s)
$fname="a0";

    open($fname, $file) || die "Could not open $file";

Quote:
}

# paste until we run out of columns
while(1)    {
    $done = 0;
    $outline = "";

        if (!eof($file))    {
            chop($line = <$file>);
            if ($file ne $Files[0]) {
                $outline = $outline . $sep . $line;
            }
            else    {
                $outline = $line;
            }
        }
        else    {
            $done = 1;
        }
    }
    last if ($done);
    print "$outline\n";

Quote:
}

# close the files

    close($file);

Quote:
}

exit;
-------------------------> CUT HERE <------------------------


Sun, 02 Feb 1997 00:13:04 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem: hash introducing extra space on output?

2. kill extra white space in a variable

3. Getting rid of extra spaces and returns

4. Extra Spaces & MSIE

5. extra spaces

6. Removing extra spaces

7. Array written to a file: Extra space inserted

8. extra space -- not expanding? [Tk-b8]

9. WIN32:CGI ActiveState(623) with IIS4.0 HTML extra output

10. Print output contains extra characters

11. Tricky column formatting/space removing question

12. DBD::CSV with column names containing spaces

 

 
Powered by phpBB® Forum Software