Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

trailing white spaces when pushing into array

by flieckster (Scribe)
on Aug 23, 2018 at 18:46 UTC ( #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 =" +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"); }


      $content = $baseurl . join("", @list);
      $content = join("", $baseurl, @list);
        Much better. I only addressed the symptom, not the real problem.
      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?

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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-06-29 00:57 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (94 votes). Check out past polls.