Trailing whitespace question

Discussion in 'Perl Misc' started by nun, Apr 2, 2007.

  1. nun

    nun Guest

    I'm reading data from a text file into an array like this:

    push @AoA, [(
    substr($_, 0, 4),
    substr($_, 4, 20),
    substr($_, 24, 30),
    substr($_, 54, 8),
    substr($_, 70, 8),
    substr($_, 103, 20),
    substr($_, 123, 1),
    )];


    I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.

    I see from Google that:

    s/ *$//;

    should do what I want, but I'm unsure how to modify my script to include
    this. Can someone help?


    DB
    nun, Apr 2, 2007
    #1
    1. Advertising

  2. nun

    Dr.Ruud Guest

    nun schreef:

    > s/ *$//;


    That should be

    s/ +$//;

    or even

    s/\s+$//;


    > substr($_, 0, 4),
    > substr($_, 4, 20),
    > substr($_, 24, 30),
    > substr($_, 54, 8),
    > substr($_, 70, 8),
    > substr($_, 103, 20),
    > substr($_, 123, 1),


    Or

    m/^(.{4})(.{20})(.{30})(.{8)).{8}(.{8)).{25}(.{20})(.)/s

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 2, 2007
    #2
    1. Advertising

  3. nun <> wrote:
    > I'm reading data from a text file into an array like this:
    >
    > push @AoA, [(
    > substr($_, 0, 4),
    > substr($_, 4, 20),
    > substr($_, 24, 30),
    > substr($_, 54, 8),
    > substr($_, 70, 8),
    > substr($_, 103, 20),
    > substr($_, 123, 1),
    > )];
    >
    >
    > I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.
    >
    > I see from Google that:
    >
    > s/ *$//;



    Removing zero spaces is rather silly.

    There is no point unless there is at least one space.


    > should do what I want, but I'm unsure how to modify my script to include
    > this. Can someone help?



    $AoA[-1][2] =~ s/ +$//;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 3, 2007
    #3
  4. nun

    Dr.Ruud Guest

    Michele Dondi schreef:
    > Michele Dondi:


    >> Or
    >> unpack 'A4A20A30A8@70A8@103A20A1' => $_;

    >
    > Or
    > unpack 'A4A20A30A8x8A8x25A20A1' => $_;
    > (And one would probably include some whitespace to improve
    > readability.)


    Are you sure "A" is powerful enough? In de documentation I see that only
    ASCII is captured.
    (perl 5.8.1, perldoc -f pack)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 3, 2007
    #4
  5. nun

    Ala Qumsieh Guest

    Michele Dondi wrote:

    > On Mon, 02 Apr 2007 11:27:11 -0400, nun <> wrote:
    >
    >>I'm reading data from a text file into an array like this:
    >>
    >> push @AoA, [(

    >
    > Why @AoA? It's just a plain array of strings...


    No. It's an array of array references. @AoA is appropriate.

    >> substr($_, 0, 4),
    >> substr($_, 4, 20),
    >> substr($_, 24, 30),
    >> substr($_, 54, 8),
    >> substr($_, 70, 8),
    >> substr($_, 103, 20),
    >> substr($_, 123, 1),
    >>)];
    >>
    >>
    >>I'd like to trim any trailing spaces from the substr($_, 24, 30) entry.

    >
    > You want to know about =~.
    >
    >>I see from Google that:
    >>
    >> s/ *$//;

    >
    > If you want to be picky, s/ *\z// would be more precise. But what you
    > wrote may be what you want. If there are no newlines involved, they
    > will be the same.


    That is not correct. The OP wants to make sure that whatever is returned by

    substr($_, 24, 30)

    does not contain trailing spaces.

    --Ala
    Ala Qumsieh, Apr 3, 2007
    #5
  6. nun

    Dr.Ruud Guest

    Dr.Ruud schreef:
    > Michele Dondi:
    >> Michele Dondi:


    >>> Or
    >>> unpack 'A4A20A30A8@70A8@103A20A1' => $_;

    >>
    >> Or
    >> unpack 'A4A20A30A8x8A8x25A20A1' => $_;
    >> (And one would probably include some whitespace to improve
    >> readability.)

    >
    > Are you sure "A" is powerful enough? In de documentation I see
    > that only ASCII is captured.
    > (perl 5.8.1, perldoc -f pack)


    Test on perl 5.8.6:

    perl -wle'
    $_ = "\x{20ac}"; # euro sign
    print length unpack "A*", $_
    '
    3

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Apr 3, 2007
    #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. Brad Marts
    Replies:
    3
    Views:
    396
    Brad Marts
    Nov 17, 2003
  2. Donald Canton

    remove trailing whitespace from string

    Donald Canton, Feb 9, 2004, in forum: C++
    Replies:
    5
    Views:
    11,168
    Tilman Kuepper
    Feb 9, 2004
  3. KevinGPO
    Replies:
    1
    Views:
    328
    Richard Herring
    Feb 20, 2006
  4. Dotan Cohen
    Replies:
    2
    Views:
    489
    Dotan Cohen
    Oct 10, 2011
  5. Taylor Strait
    Replies:
    13
    Views:
    360
    Chris Gernon
    Dec 28, 2006
Loading...

Share This Page