Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Backspace characters in perl input

SOLVED
Go to Solution
Valued Contributor Valued Contributor

Backspace characters in perl input

I have written a perl script that accepts input
from the user in the form of:
chomp($indata = );
which works just fine, except that it the user
types a backspace because he misspelled a word,
the backspace characters show up in the complete text! If what is being entered is a file name, the file can not be found! How can I get perl to read the data already collapsed?
Or, how can I collapse it before saving it?

Thanks for your assistance,
jls
5 REPLIES
Honored Contributor Honored Contributor

Re: Backspace characters in perl input

# Step 1. remove all backspaces from string start
$indata =~ s/^\b+//;
# Step 2. remove all backspace
1 while $indata =~ s/.\b//;


FWIW $indata =~ s/.\b//g; would not work, because that would fail to remove

"abcd\b\b\bxx"


Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Valued Contributor Valued Contributor

Re: Backspace characters in perl input

That didn't work :-(

When I enter: myfleile
I get nothing (null?) after those commands.

What I want to get is: myfile

How can I get that? Even as I read your code, I am not sure I would have got what I wanted anyway. It looks like I'd get: myfleile, which isn't what I need.

jls
Honored Contributor Honored Contributor

Re: Backspace characters in perl input

D'uh (/me curses) please excuse me. Should not have happened.

lt09:/tmp 107 > perl -de 1

Loading DB routines from perl5db.pl version 1.23
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
DB<1> $indata = "myfle^H^Hile"

DB<2> x $indata
0 "myfle\cH\cHile"
DB<3> $indata =~ s/^\b+//

DB<4> x $indata
0 "myfle\cH\cHile"
DB<5> 1 while $indata =~ s/.\b//

DB<6> x $indata
0 ''
DB<7>

Now I see that I erroneously misused \b as backspace, which it is not in a regex.

chomp ($indata = "myfle\b\bile"); # Here it *is* a backspace
$indata =~ s/^\cH+//; # Ctrl-H is backspace
1 while $indata =~ s/.[\b]//; # \b also is backspace inside character class

DB<7> chomp ($indata = "myfle\b\bile"); # Here it *is* a backspace

DB<8> x $indata
0 "myfle\cH\cHile"
DB<9> $indata =~ s/^\cH+//; # Ctrl-H is backspace

DB<10> x $indata
0 "myfle\cH\cHile"
DB<11> 1 while $indata =~ s/.[\b]//; # \b also is backspace inside character class

DB<12> x $indata
0 'myfile'
DB<13>

Enjoy, Have FUN! H.Merijn [ 0 points for the first answer. I feel ashamed! ]
Enjoy, Have FUN! H.Merijn
Valued Contributor Valued Contributor

Re: Backspace characters in perl input

That did it! I am not sure how it works, but right now I don't care, as long as it works!

You get 2 points for the first try. I don't anyone should get 0 points if they respond to a help request :-)

Thanks much
jls
Regular Advisor

Re: Backspace characters in perl input

So, what does code look like exacly?