Results 1 to 3 of 3
I have a line containing junk characters e.g. ^D^H^HHello world,
I am doing
$i = s/\^D//ig;
$i = s/\^H//ig;
is there any better way to remove only these junk char?...
- 04-17-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 19
Is there any way remove all junk char from a string/line using perl?
I have a line containing junk characters e.g. ^D^H^HHello world,
I am doing
$i = s/\^D//ig;
$i = s/\^H//ig;
is there any better way to remove only these junk char?
- 04-17-2008 #2
Sure is:
We are using the 'tr' operator here. You give tr two lists, and it maps each character from the first list to the corresponding character of the second list. For instance:Code:$i =~ tr/a-zA-Z0-9 _,.-//cd
would translate every 'a' to 'A', and every 'b' to 'B'.Code:tr /ab/AB/
We are using two special options here. The first is /c. /c means to take the complement of the first set: so rather than use the first set, use everything that is not in the first set. This allows us to specify what we want to keep, and get rid of everything else.
The second option is /d. /d means that if a character does not have a corresponding character in the second set (imagine, for instance, that the second set is smaller than the first), you should just delete the character (default behavior is to use the last character in the second set repeatedly).
So the above command says to take every character that is not one that we specified, and delete it.
Hope that helps!DISTRO=Arch
Registered Linux User #388732
- 04-17-2008 #3Just Joined!
- Join Date
- Apr 2008
- Posts
- 19
yeah, it works it deletes all junk char but now I realised that my hash is also not able to store whole string containing junk value.
I am retrieving the data from XML file and there is a data which has junk char. Looks like hash which i am using for storing the values itself is not picking whole string containing junk values.
Can I do something so that is should pick the whole string?


Reply With Quote