escape sequence for tab not working

Discussion in 'Perl Misc' started by dn_perl@hotmail.com, Dec 9, 2003.

  1. Guest

    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.
     
    , Dec 9, 2003
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> 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
     
    Anno Siegel, Dec 9, 2003
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 9, 2003
    #3
  4. Guest

    -berlin.de (Anno Siegel) wrote in message news:<br4881$jvg$-Berlin.DE>...
    > <> 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.
     
    , Dec 9, 2003
    #4
  5. naniwadekar Guest

    "Tad McClellan" <> 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.
     
    naniwadekar, Dec 10, 2003
    #5
  6. Tintin Guest

    "naniwadekar" <> wrote in message
    news:br6bh9$293aem$-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.
     
    Tintin, Dec 10, 2003
    #6
  7. Anno Siegel Guest

    naniwadekar <> wrote in comp.lang.perl.misc:
    >
    > "Tad McClellan" <> 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
     
    Anno Siegel, Dec 10, 2003
    #7
  8. Guest

    "naniwadekar" <> wrote in message news:<br6bh9$293aem$-berlin.de>...
    > "Tad McClellan" <> 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 pissed 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.
     
    , Dec 10, 2003
    #8
  9. On Tue, 9 Dec 2003, naniwadekar wrote:

    > I re-typed the perl code

    [..]

    > I need have no regrets.


    The result is inevitable. Bye.
     
    Alan J. Flavell, Dec 10, 2003
    #9
  10. naniwadekar <> wrote:
    > "Tad McClellan" <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 10, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. h babu
    Replies:
    1
    Views:
    426
    Vidar Petursson
    Jul 16, 2003
  2. marcia

    escape sequence \n

    marcia, Aug 28, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    570
    marcia
    Aug 28, 2003
  3. Jeff

    Unrecognized escape sequence

    Jeff, Jun 30, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    39,151
    mansoorbhai
    Jun 21, 2011
  4. slomo
    Replies:
    5
    Views:
    1,627
    Duncan Booth
    Dec 2, 2007
  5. Replies:
    3
    Views:
    106
    Randy Webb
    Aug 13, 2005
Loading...

Share This Page