CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need to reomve leading whitespace

 



tester_V
Novice

Nov 19, 2017, 5:32 PM

Post #1 of 4 (1257 views)
Need to reomve leading whitespace Can't Post

I made a mistake. I'm having problem copying because the array list file has a white space before all the lines but the first on.

I thinks it is Sort::Naturally adds the white space.

Do you guys know how to remove it?

Here is the code I'm using:

#!/usr/local/bin/perl
use strict ;
use warnings ;
use File::Copy;
use Sort::Naturally ;

my $low = "C:/Sixshot_logs/Flow.txt" ;
my $flow_sorted = "C:/Sixshot_logs/Flow-Sorted.txt" ;


open (my $flow_sorted_fh,'>',$flow_sorted) or die "Cannot open $flow_sorted $!" ;

open (my $low_fh, '<', $low) or die $!;
my @file_list = <$low_fh>;

my @list = nsort (@file_list) ;
pop @list;

print "@list\n" ;
print $flow_sorted_fh "@list" ;

close $low_fh ;
close $flow_sorted_fh ;

And the Array list log file and the second line up to the end has a leading whitespace:

C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-01.1.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-01.2.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-01.3.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-01.4.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-01.5.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-02.1.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-02.2.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-02.3.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-02.4.log
C:/Sixshot_logs/SixShot/Tell_flow_log.2017-10-02.5.log

Code



      
    


tester_V
Novice

Nov 19, 2017, 5:58 PM

Post #2 of 4 (1256 views)
Re: [tester_V] Need to reomve leading whitespace [In reply to] Can't Post

CrazyGod damn it!
In my previous post I think Laurent already pined me this little trick.
I added it to my code and everything is working now.

Here is what I added:

chomp @list ;
print $flow_sorted_fh "$_\n" for @list;

Code



      
    


Laurent_R
Veteran / Moderator

Nov 19, 2017, 11:18 PM

Post #3 of 4 (1250 views)
Re: [tester_V] Need to reomve leading whitespace [In reply to] Can't Post

When your program misbehave, goes nuts or fails, very often, it is because your variable don't contain what you think. One basic debugging technique is to take the habit to print the contents of your variables.

For this you can use a debugger, but there are some other very basic ways.

To really know the content of a variable (say $foo) just add a print statement like this to you code:


Code
print "<$foo>";

This way, if your program prints:

Code
<bar 
>
# Or
< bar>

you know that you have an unexpected trailing end of line character or leading white space.

For variable containing more complicated things such as arrays, hashes of other data structure, use the Data::Dumber module (which is core, so should be available on any Perl install).


tester_V
Novice

Nov 20, 2017, 8:21 PM

Post #4 of 4 (1241 views)
Re: [Laurent_R] Need to reomve leading whitespace [In reply to] Can't Post

And again, THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!
THANK YOU Laurent!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives