removing spaces from the input text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raj4perl
    New Member
    • Aug 2014
    • 12

    removing spaces from the input text

    open(HNDL, "1.txt") or die "Cannot open text file";

    while (<HNDL>)
    {
    if($.==2)
    {
    print $_;
    $_ =~ s/\s//g;
    print "after removing spaces $. :",$_;
    }
    }

    output --
    " 1 0 0 "

    Actually, I need it as "100". So that I can perform some arithmetic operations.

    Excuse if there is silly mistakes in using substitute. I'm just a beginner in perl..!!
  • vinothree
    New Member
    • Aug 2014
    • 5

    #2
    Hi Raj,

    I have checked your script with DATA handler as below, it is working fine.
    Code:
    use strict;
    use warnings;
    
    while (<DATA>)
    {
            if($.==2)
            {
                    print $_;
                    $_ =~ s/\s//g;
                    print "after removing spaces $. :",$_;
            }
    }
    __DATA__
    1000
    1 0 0
    I suspect your file content, could you please show us your file content ?
    Last edited by vinothree; Aug 11 '14, 07:47 AM. Reason: type error

    Comment

    • Raj4perl
      New Member
      • Aug 2014
      • 12

      #3
      Hi Vino,

      Thanks for the reply..!!

      here's the content.. I'm sending you little part of it.

      --content in text file --

      "XXXXXXXXXXXRem aining
      100 "

      these are the first two lines of the text file

      Comment

      • vinothree
        New Member
        • Aug 2014
        • 5

        #4
        Ok, Lets check this way,

        1.First confirm that "1 0 0" comes in the second line only.
        2.From your sample input, I can see that, 100 spaces removed already?

        Comment

        • Raj4perl
          New Member
          • Aug 2014
          • 12

          #5
          1.First confirm that "1 0 0" comes in the second line only.
          [Raj] Yes, 1 0 0 comes in the second line itself

          2.From your sample input, I can see that, 100 spaces removed already?
          [Raj] In the text file spaces are not there. But, when you read it to a variable by using above script.. the variable is storing with a space for each character.

          Even if i print first line, output will be

          X X X X X R E M A N I N G

          Comment

          • vinothree
            New Member
            • Aug 2014
            • 5

            #6
            Interesting!
            1.What is the perl version you are using ?
            2.How you code this 1.txt file, which generated by any other scripts ?
            3.If you are from linu/unix execute this "file 1.txt" and let me know the output.

            Comment

            • Raj4perl
              New Member
              • Aug 2014
              • 12

              #7
              1.What is the perl version you are using ?
              Perl 5.16

              2.How you code this 1.txt file, which generated by any other scripts ?
              it's an internal tool generated from batch file

              3.If you are from linu/unix execute this "file 1.txt" and let me know the output.
              I'm using Windows

              -Raj

              Comment

              • vinothree
                New Member
                • Aug 2014
                • 5

                #8
                I got you now, as you are windows, your file will be in different encoding.
                Try this,

                Code:
                open my $fh, '<:encoding(UTF-16LE)', 1.txt or die "Couldn't read '1.txt': $! / $^E";
                while (<$fh>) {
                    print $_;
                };
                Last edited by vinothree; Aug 12 '14, 04:56 AM. Reason: Added code tag

                Comment

                • Raj4perl
                  New Member
                  • Aug 2014
                  • 12

                  #9
                  Thanks Vinoth.. That's the problem.. I have tried saving the file in ANSI encoding format and now it's working..

                  But, I think we need a encoding module to be dowloaded right?

                  Comment

                  • vinothree
                    New Member
                    • Aug 2014
                    • 5

                    #10
                    I do not think you need additioal modules as you are using the latest perl version only, if needed you can install it.

                    Comment

                    • Raj4perl
                      New Member
                      • Aug 2014
                      • 12

                      #11
                      OK.. anyways.. it's working now..Thanks a lot..!!

                      Comment

                      Working...