missing 0s

Discussion in 'Perl Misc' started by Min Wang, Sep 1, 2004.

  1. Min Wang

    Min Wang Guest

    I have line in a file
    001-02-0003

    I do
    ($a,$b,$c) = split('-',$_) after read.
    Now I print them I get 1,2,3 instead of 001,02,0003.

    Please help and thanks.
    Min Wang, Sep 1, 2004
    #1
    1. Advertising

  2. Min Wang wrote:
    > I have line in a file
    > 001-02-0003
    >
    > I do
    > ($a,$b,$c) = split('-',$_) after read.
    > Now I print them I get 1,2,3 instead of 001,02,0003.


    $_ = '001-02-0003';
    my ($x,$y,$z) = split /-/;
    print "$x,$y,$z\n";

    Outputs:
    001,02,0003

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 2, 2004
    #2
    1. Advertising

  3. Min Wang

    Anno Siegel Guest

    Min Wang <> wrote in comp.lang.perl.misc:
    > I have line in a file
    > 001-02-0003
    >
    > I do
    > ($a,$b,$c) = split('-',$_) after read.
    > Now I print them I get 1,2,3 instead of 001,02,0003.


    How do you print them? Explaining in English what you (think you) did
    is insufficient. Show your code!

    If you print them in a normal print-statement immediately after the
    split, the zeroes will show up. Gunnar has demonstrated that in
    his reply. So what else are you doing? Are you using printf for
    printing? With what format? Is there intervening code between the
    split() and print()?

    Anno
    Anno Siegel, Sep 2, 2004
    #3
  4. Min Wang

    Min Wang Guest

    Gunnar,

    Thank you very much.

    Furthermore,

    I tried

    $x = substr($b, 0, 1);
    $y = substr($b, 1);

    I expect to have $x=0 and $y=2.
    In stead, I got $x = 2 and $y is null.

    Thanks.

    Gunnar Hjalmarsson <> wrote in message news:<>...
    > Min Wang wrote:
    > > I have line in a file
    > > 001-02-0003
    > >
    > > I do
    > > ($a,$b,$c) = split('-',$_) after read.
    > > Now I print them I get 1,2,3 instead of 001,02,0003.

    >
    > $_ = '001-02-0003';
    > my ($x,$y,$z) = split /-/;
    > print "$x,$y,$z\n";
    >
    > Outputs:
    > 001,02,0003
    Min Wang, Sep 2, 2004
    #4
  5. Min Wang wrote:
    > Gunnar Hjalmarsson wrote:
    >> Min Wang wrote:
    >>> I have line in a file
    >>> 001-02-0003
    >>>
    >>> I do
    >>> ($a,$b,$c) = split('-',$_) after read.
    >>> Now I print them I get 1,2,3 instead of 001,02,0003.

    >>
    >> $_ = '001-02-0003';
    >> my ($x,$y,$z) = split /-/;
    >> print "$x,$y,$z\n";
    >>
    >> Outputs:
    >> 001,02,0003

    >
    > Thank you very much.
    >
    > Furthermore,
    >
    > I tried
    >
    > $x = substr($b, 0, 1);
    > $y = substr($b, 1);
    >
    > I expect to have $x=0 and $y=2.
    > In stead, I got $x = 2 and $y is null.


    my $first = substr $y, 0, 1;
    my $second = substr $y, 1;
    print "First: $first, second: $second\n";

    Outputs:
    First: 0, second: 2

    Funny computer you have. :)

    How about following Anno's advice and post the exact code that
    produces the results you say it does?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 2, 2004
    #5
  6. Min Wang

    Sam Holden Guest

    On 2 Sep 2004 04:41:01 -0700, Min Wang <> wrote:
    > Gunnar,
    >
    > Thank you very much.
    >
    > Furthermore,
    >
    > I tried
    >
    > $x = substr($b, 0, 1);
    > $y = substr($b, 1);
    >
    > I expect to have $x=0 and $y=2.
    > In stead, I got $x = 2 and $y is null.


    No you don't:

    $_ = '001-02-0003';
    ($a,$b,$c) = split('-',$_);
    $x = substr($b, 0, 1);
    $y = substr($b, 1);
    print "*$x* *$y*\n";

    Stop posting snippets of code and instead post a small self contained
    script that demonstrates the problem. It'll only be a few lines long...

    [snip a full quote, from which I retrieved the $_ = and split bits]

    --
    Sam Holden
    Sam Holden, Sep 2, 2004
    #6
    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. Paul Fox
    Replies:
    0
    Views:
    744
    Paul Fox
    Mar 5, 2004
  2. Mark Newby
    Replies:
    1
    Views:
    1,696
  3. CodeMonkey
    Replies:
    0
    Views:
    406
    CodeMonkey
    Oct 11, 2005
  4. Acceed See
    Replies:
    1
    Views:
    523
    Carl W.
    Apr 22, 2005
  5. Gary Herron
    Replies:
    2
    Views:
    662
    Bruno Desthuilliers
    Jul 4, 2006
Loading...

Share This Page