Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Why do spaces keep getting added after I print the elements of an array?

by scottstef (Curate)
on Jun 27, 2001 at 22:42 UTC ( #92034=perlquestion: print w/replies, xml ) Need Help??

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, <> Fool,Yuri A., <> Stupid,Issac A., <>
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 , Fool , Yuri A. , Stupid , Issac A. ,

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"; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://92034]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-08-16 07:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found