http://www.perlmonks.org?node_id=92034

scottstef has asked for the wisdom of the Perl Monks concerning the following question:

Greetings friends, I did a super search for removing spaces, and wasn't quite sure how it carried over to an array. I am trying to read data from a file, split it, into an array and then print elements into another file.

Data format:

Dummy,Big I, <bdummy@name.com> Fool,Yuri A., <yfool@name.com> Stupid,Issac A., <istupid@name.com>
Here is the code i am using:

#!d:\perl.exe -w use strict; use warnings; my $oldfile = $ARGV[0]; my $newfile = $oldfile . ".clean.txt"; open (OLDFILE, "$oldfile") || die "Could not open $oldfile"; open (NEWFILE, ">$newfile") || die "Could not open $newfile"; while (<OLDFILE>) { my @record = split /(["<",">"])/; print NEWFILE "@record[0...4,]"; print NEWFILE "$record[6]\n"; } close OLDFILE; close NEWFILE;

Here is the data that gets generated:

Dummy , Big I , bdummy@name.com Fool , Yuri A. , yfool@name.com Stupid , Issac A. , istupid@name.com

I keep getting spaces added after the fields.

"The social dynamics of the net are a direct consequence of the fact that nobody has yet developed a Remote Strangulation Protocol." -- Larry Wall

Replies are listed 'Best First'.
Re: Why do spaces keep getting added after I print the elements of an array?
by japhy (Canon) on Jun 28, 2001 at 00:43 UTC
    Because printing an array or array slice or hash slice inside double quotes is the same as join $", (...); and $" is " ". Perhaps you'll want to do: local $";.

    japhy -- Perl and Regex Hacker

      Or just drop the quotes since print can take a list of values.

              - tye (but my friends call me "Tye")
Re: Why do spaces keep getting added after I print the elements of an array?
by chipmunk (Parson) on Jun 28, 2001 at 01:53 UTC
    The issue with $" has already been answered, but I just wanted to put out something about the regex you're splitting on: my @record = split /(["<",">"])/; You've got a character class there, with some redundant characters. It's actually equivalent to the character class ["<>,]. I'm not sure if that's what you meant with the quotes and the comma. You're also getting the delimiters in your resulting array, because of the capturing parens in the regex.

    Here's another way of solving the problem, that I think is a tiny bit simpler:

    while (<OLDFILE>) { chomp; my($last, $first, $email) = split /, */; $email =~ tr/<>//d; print NEWFILE "$last, $first, $email\n"; }