#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    How to get rid of white spaces in perl array.


    Hello,

    This is not to get rid of training or leading white spaces. I am importing an input file into an array and I cannot seem to rid of the spaces in between.

    The input file looks like this:
    --------------------------------------------------------------
    #1 Change from native to multi
    a0 a4 00 00 02 3f 02 a2 a4 10 20 02 7f bd ac a4 11 00 02 6f 60 10 d6 11 01 01 03 a0 d6 c0 09 08 03 00 0f 3c 0e ff ff b8
    #2 Download 1
    a1 b4 11 00 12 3f 00 ab a4 00 22 02 7f be a0 e5 07 80 02 6f 21 a0 e0 00 80 33 62 31 82 02 01 21 83 02 6f 21 8a 01 05 ab 16 80 01 03 a4 06 83 01 01 90 00 18 80 01 18 a4 06 83 01 0b 95 01 08 80 02 000b 88 00 b5 10 c0 01 10 da 01 99
    ....
    cont...
    --------------------------------------------------------------

    I do not want to do anything to the lines that start with a "#" only get rid of spaces between the bytes.

    My code:
    #!/usr/bin/perl

    use strict;
    #use warnings;
    #use diagnostics;

    # Global Vars
    my $IF_name = 'APDU_List_Test.txt';

    open FILE1, "+<$IF_name" or die "Failed to open file $!";

    #Dump FILE1 to an array
    my @file_content;
    @file_content = <FILE1>;

    my $line;
    my $Output_File_Name = "Results.txt";

    open(FILE2, ">$Output_File_Name");
    foreach $line (@file_content) {
    chomp($line);
    #Remove leading spaces
    $line =~ s/^\s+//;
    #Remove trailing spaces
    $line =~ s/\s+$//;
    print FILE2 $line;
    print "\n";
    }

    print "Done.\n";

    Output file should look like this:
    --------------------------------------------------------------
    #1 Change from native to multi
    a0a40000023f02a2a41020027fbdaca41100026f6010d611010103a0d6c0090803000f3c0effffb8
    #2 Download 1
    1a1b41100123f00aba40022027fbea0e50780026f21a0e000803362318202012183026f218a0105ab16800103a4068301019 00018800118a40683010b95010880
    02000b8800b510c00110da0199
    ....

    cont...
    -------------------------------------------------------------

    Instead it looks like this:
    -------------------------------------------------------------
    #1 Change from native to multi-a0 a4 00 00 02 3f 00 a0 a4 00 00 02 7f bd a0 a4 00 00 02 6f 60 a0 d6 00 01 01 03 a0 d6 00 09 08 03 00 0f 3c 00 ff ff 08#2 Download 1a0 a4 00 00 02 3f 00 a0 a4 00 00 02 7f bd a0 e4 00 00 02 6f 21 a0 e0 00 00 33 62 31 82 02 01 21 83 02 6f 21 8a 01 05 ab 16 80 01 03 a4 06 83 01 01 95 01 08 80 01 18 a4 06 83 01 0b 95 01 08 80 02 000b 88 00 a5 06 c0 01 00 de 01 88

    I tried using JOIN as well, but the output file came out more messed up than this.

    Please advise.
    Thanks.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,274
    Rep Power
    0
    This might work:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    while (<DATA>) {
    	unless (/^#/) {
    		tr/ //d;
    	}
    	print;
    }
    
    __DATA__
    #1 Change from native to multi
    a0 a4 00 00 02 3f 02 a2 a4 10 20 02 7f bd ac a4 11 00 02 6f 60 10 d6 11 01 01 03 a0 d6 c0 09 08 03 00 0f 3c 0e ff ff b8
    #2 Download 1
    a1 b4 11 00 12 3f 00 ab a4 00 22 02 7f be a0 e5 07 80 02 6f 21 a0 e0 00 80 33 62 31 82 02 01 21 83 02 6f 21 8a 01 05 ab 16 80 01 03 a4 06 83 01 01 90 00 18 80 01 18 a4 06 83 01 0b 95 01 08 80 02 000b 88 00 b5 10 c0 01 10 da 01 99

    Comments on this post

    • Laurent_R agrees

IMN logo majestic logo threadwatch logo seochat tools logo