Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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, <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

Comment on Why do spaces keep getting added after I print the elements of an array?
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-25 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls