Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

trailing white spaces when pushing into array

by flieckster (Scribe)
on Aug 23, 2018 at 18:46 UTC ( [id://1220945]=perlquestion: print w/replies, xml ) Need Help??

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

HI there. i've tried chomp, trim, and string::util text::trim and i can't seem to get anything to work. I'm simply globing a directory of files into an array, then adding a "," to them and printing out the list. i am pushing the list of filenames with the comma added into an array using PUSH, but when i go to print the filenames back out their is trailing white space. But on line 30 when i print the filenames before they are pushed into the array the white space isn't there. i want to use the $baseurl, and list to produce a list 123.psd,345.psd,543.psd not 123.psd, 345.psd, 543.psd
#!/usr/bin/perl -w use File::Find; use File::Copy; use Net::SMTP; use File::Basename; use File::Slurp; use Net::FTP; use Email::Send::SMTP::Gmail; use String::Util ':all'; use LWP::Simple; use Cwd; chomp($myname = `id -un`); my $dir ="/Users/$myname/Desktop/"; my $logname = 'content.csv'; my $output = "> /Users/$myname/Desktop/$logname"; use POSIX qw(strftime); my $date = strftime("%m-%d-%y",localtime); my $time = strftime("%I:%M:%S",localtime); my $comma = ","; my $folder ="/Volumes/photorepos/Partners/NYCompany/post/instock/RUSH/ +"; chdir ($folder); (@files) = glob "*.psd"; foreach $file (@files){ print "$file".','; push @list,("$file".','); crunch(@list); }; my $baseurl ="http://trueaction.workhorsegroup.us/ServerExport.cfm?Con +figFile=ServerExportCfg-NYandCFileStatus.inc&FormatOverride=CSV&filen +ames="; open FILE, $output or warn "$!"; $content = ("$baseurl@list"); # print FILE "$content"; close FILE;

Replies are listed 'Best First'.
Re: trailing white spaces when pushing into array
by BillKSmith (Monsignor) on Aug 23, 2018 at 19:26 UTC
    The space you see is the default value of LIST_SEPARATOR ($"). Set it locally to a null string. See perldoc -v "LIST_SEPARATOR".
    { local $"=''; $content = ("$baseurl@list"); }
    Bill

      yuck!

      $content = $baseurl . join("", @list);
      or
      $content = join("", $baseurl, @list);
        Much better. I only addressed the symptom, not the real problem.
        Bill
      ohh, thanks! that worked great!
Re: trailing white spaces when pushing into array
by soonix (Canon) on Aug 23, 2018 at 19:26 UTC
    • you didn't tell us what your crunch sub does, it might be the culprit adding spaces.
    • search for $" in perlvar: if you use the array inside a string "@list" perl puts the "list separator" (by default a space) between the elements - you have
      $content = ("$baseurl@list");
      This is where it happens.
      crunch did nothing actually. i'll look into the perlvar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1220945]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found