473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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..!!
Aug 11 '14 #1
10 1257
Hi Raj,

I have checked your script with DATA handler as below, it is working fine.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. while (<DATA>)
  5. {
  6.         if($.==2)
  7.         {
  8.                 print $_;
  9.                 $_ =~ s/\s//g;
  10.                 print "after removing spaces $. :",$_;
  11.         }
  12. }
  13. __DATA__
  14. 1000
  15. 1 0 0
  16.  
I suspect your file content, could you please show us your file content ?
Aug 11 '14 #2
Hi Vino,

Thanks for the reply..!!

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

--content in text file --

"XXXXXXXXXXXRemaining
100 "

these are the first two lines of the text file
Aug 11 '14 #3
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?
Aug 11 '14 #4
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
Aug 11 '14 #5
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.
Aug 11 '14 #6
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
Aug 11 '14 #7
I got you now, as you are windows, your file will be in different encoding.
Try this,

Expand|Select|Wrap|Line Numbers
  1. open my $fh, '<:encoding(UTF-16LE)', 1.txt or die "Couldn't read '1.txt': $! / $^E";
  2. while (<$fh>) {
  3.     print $_;
  4. };
Aug 11 '14 #8
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?
Aug 11 '14 #9
I do not think you need additioal modules as you are using the latest perl version only, if needed you can install it.
Aug 11 '14 #10
OK.. anyways.. it's working now..Thanks a lot..!!
Aug 12 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
12
by: Magix | last post by:
Hi, Everytime I received a fix-length of string, let say 15 (the unused portion will filled with Spaces before receive), I want to remove the Spaces from END until I encounter a non-space char....
3
by: Ali | last post by:
I have 3 html input tex in my asp.net form. Two of them are calling javascript client side to calculate the differnce of two dates and put the result into the third input text. i haven't include...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
3
by: kvnsmnsn | last post by:
I've written the following Javascript file that includes an input text field and an output text field, the latter of which is initialized to zero. Each time the user enters a number in the input...
3
by: John Grewar | last post by:
Hello, In MS Access I have a field that contains data which looks like this example, GZQ 678239; GZQ 014312; etc. How do I remove the spaces to make it become (not just look like) GZQ678239;...
2
by: =?Utf-8?B?R3VoYW5hdGg=?= | last post by:
Hi, In a web page we have many single line input text boxes inside table,which has the max length of 250 characters and few drop downs where auto post back property is true. When we provide 250...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
4
by: backups2007 | last post by:
I want to be able to pass rows of queried data to rows of input text boxes. As the example below shows, I have come up with this incomplete solution. But this code only passes the data to the first...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.