Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > escape sequence for tab not working

Reply
Thread Tools

escape sequence for tab not working

 
 
dn_perl@hotmail.com
Guest
Posts: n/a
 
      12-09-2003
I am unable to print a tab using "\t" sequence in a file
which has some database related statements.

I am using perl on a solaris server with Oracle.

In some_dbi.pl, the patch is : \t prints as single character.
--------
my $this_str, $sub_str ;
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
system( "echo $this_str") ;
--------




In ch.pl, the patch is : \t prints as tab-character.
--------
$sub_str = "abc" ;
$this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
print $this_str ;
--------

What could be causing this behaviour? I want \t to print
as tab .

TIA.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      12-09-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I am unable to print a tab using "\t" sequence in a file
> which has some database related statements.
>
> I am using perl on a solaris server with Oracle.
>
> In some_dbi.pl, the patch is : \t prints as single character.


I don't think "patch" is the word you want to use here.

The behavior of your code is correct and must be expected.

> --------
> my $this_str, $sub_str ;


There are parentheses missing around the variables. Are you running
under strict and warnings? You should.

> $sub_str = "abc" ;
> $this_str = sprintf "%s\t%s", $sub_str, $sub_str ;


This sets $this_str to "abc", followed by a literal tab, followed by
"abc".

> system( "echo $this_str") ;
> --------


When you use system() in this form, it calls a shell to execute the
command given. The string the shell sees is "echo abc abc", where
the white space between the two "abc" is a tab. The shell, however,
doesn't care in what form or amount whitespace is given between words,
it just splits the command into words. So echo doesn't get to see
a tab at all, all it knows it has to echo the word "abc" twice, and
that's what it does.

> In ch.pl, the patch is : \t prints as tab-character.
> --------
> $sub_str = "abc" ;
> $this_str = sprintf "%s\t%s", $sub_str, $sub_str ;
> print $this_str ;
> --------


Here Perl prints the value without shell intervention. The tab character
is displayed as such.

> What could be causing this behaviour? I want \t to print
> as tab .


Why do you want to print the string via system() and "echo"? Perl
can print strings just fine. I don't think you'll need the following
information.

Your question is more about the shell than about Perl. You need to find
out how to make your shell's "echo" (or the system's /bin/echo) print a
tab character. For my sh, that's 'echo -e "abc\tabc"' (note inner quotes).

That string, in Perl, is written $str = '"abc\\tabc"'. The "\" must
be doubled because it is a quoting character even in single quotes.
So your sprintf should look like

$this_str = sprintf '"%s\\t%s"', $sub_str, $sub_str;

Then

system "echo $this_str";

will print the tab.

Anno



 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-09-2003
(E-Mail Removed) <(E-Mail Removed)> wrote:


> my $this_str, $sub_str ;



Do not re-type Perl code

Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.


Like this one.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      12-09-2003
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<br4881$jvg$(E-Mail Removed)-Berlin.DE>...
> (E-Mail Removed) <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> > $sub_str = "abc" ;
> > $this_str = sprintf "%s\t%s", $sub_str, $sub_str ;

>
> This sets $this_str to "abc", followed by a literal tab, followed by
> "abc".
>
> > system( "echo $this_str") ;


> Your question is more about the shell than about Perl. You need to find
> out how to make your shell's "echo" (or the system's /bin/echo) print a
> tab character. For my sh, that's 'echo -e "abc\tabc"' (note inner quotes).


For my sh a real tab character escaped with \ works too.

So in general you can protect $this_str from being word-split by the
shell using quotemeta().

system( "echo \Q$this_str\E"); # \E technically redundant

BTW: Someone whose oppinion I usually respect once told me the above
will not always work but was unable to offer an example where it
wouldn't.
 
Reply With Quote
 
naniwadekar
Guest
Posts: n/a
 
      12-10-2003

"Tad McClellan" <(E-Mail Removed)> wrote -
>
> Do not re-type Perl code
>
> Use copy/paste or your editor's "import" function rather than
> attempting to type in your code. If you make a typo you will get
> followups about your typos instead of about the question you are
> trying to get answered.
>


I re-typed the perl code because I can't access internet
from my workstation at my workplace. But since the
followup tackled the typo AND (not INsteaDOf) my
question, I need have no regrets. *This* time, at any rate.

I do run my code under strict but I excuse myself from
re-typing it when I submit queries to the forum.

To Anno Siegel - Thanks for the help.
I did not use perl's print because I wanted the output to
go to a file instead of stdout.

Thanks, again.


 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      12-10-2003

"naniwadekar" <(E-Mail Removed)> wrote in message
news:br6bh9$293aem$(E-Mail Removed)-berlin.de...
>
> To Anno Siegel - Thanks for the help.
> I did not use perl's print because I wanted the output to
> go to a file instead of stdout.


Believe it or not, Perl can actual output to files too.

It would be a little limiting having a language that only deals with
stdin/stdout.


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      12-10-2003
naniwadekar <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "Tad McClellan" <(E-Mail Removed)> wrote -
> >
> > Do not re-type Perl code
> >
> > Use copy/paste or your editor's "import" function rather than
> > attempting to type in your code. If you make a typo you will get
> > followups about your typos instead of about the question you are
> > trying to get answered.
> >

>
> I re-typed the perl code because I can't access internet
> from my workstation at my workplace.


Then test it again before you send it.

> But since the
> followup tackled the typo AND (not INsteaDOf) my
> question, I need have no regrets. *This* time, at any rate.


Great. You can't be bothered to make sure the code you submit is
correct. Never mind if people have to correct few spurious errors
along the way, as long as they also answer the original question.

It's nice to learn that you don't have regrets, but I do.

> I do run my code under strict but I excuse myself from
> re-typing it when I submit queries to the forum.


Oh no. It's far easier to make a dozen experts find your stricture
violations manually.

> To Anno Siegel - Thanks for the help.


Thanks for making your attitude clear.

> I did not use perl's print because I wanted the output to
> go to a file instead of stdout.


Nonsense.

Anno
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      12-10-2003
"naniwadekar" <(E-Mail Removed)> wrote in message news:<br6bh9$293aem$(E-Mail Removed)-berlin.de>...
> "Tad McClellan" <(E-Mail Removed)> wrote -
> >
> > Do not re-type Perl code
> >
> > Use copy/paste or your editor's "import" function rather than
> > attempting to type in your code. If you make a typo you will get
> > followups about your typos instead of about the question you are
> > trying to get answered.
> >

>
> I re-typed the perl code because I can't access internet
> from my workstation at my workplace.


Could I suggest removable media?

> But since the followup tackled the typo AND (not INsteaDOf) my
> question, I need have no regrets.


Believing that you need have no regets because you got what you wanted
even though you ****ed people off in the process is not something that
most people would mention in public. (Because it makes you sound like
a bit of a sociopath).

> I do run my code under strict but I excuse myself from
> re-typing it when I submit queries to the forum.


Indeed, there's no moral requirement to actually include "use strict"
and "use warnings" in the code fragments you post. There is a moral
requirement to ensure there are not mistakes that would have been
detected by their use.

> To Anno Siegel - Thanks for the help.


Usenet is a threaded medium. If you wish to follow-up stuff in Anno's
response you should do so in a follow-up to Anno's response.

> I did not use perl's print because I wanted the output to
> go to a file instead of stdout.


Perl's print() is the usual way to write to files in Perl.
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      12-10-2003
On Tue, 9 Dec 2003, naniwadekar wrote:

> I re-typed the perl code

[..]

> I need have no regrets.


The result is inevitable. Bye.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-10-2003
naniwadekar <(E-Mail Removed)> wrote:
> "Tad McClellan" <(E-Mail Removed)> wrote -
>>
>> Do not re-type Perl code



> But since the
> followup tackled the typo AND (not INsteaDOf) my
> question, I need have no regrets.



Thank you for making your selfishness so explicit,
I now know what I need to do.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
tab escape not shown in textarea soup_or_power@yahoo.com Javascript 3 08-13-2005 05:01 PM
escape sequence branigan Cisco 4 04-15-2005 02:14 PM
escape sequence \n marcia ASP .Net 4 08-28-2003 07:16 PM
Escape Sequence to printer from Web Page h babu ASP .Net 1 07-16-2003 08:08 PM



Advertisments