string manipulation

Discussion in 'Perl Misc' started by a, Jan 27, 2006.

  1. a

    a Guest

    Hi,
    I have an input string. It is a full path of a file.
    eg. //server/dirA/dirB/file.txt
    How can I parse it into directory and file?
    i.e //server/dirA/dirB/ and file.txt

    Also, I have a directory as an input.
    e.g //server/dirA/dirB/
    How can I check the last character is a / or not?
    Thanks a lot
    a, Jan 27, 2006
    #1
    1. Advertising

  2. a

    John Bokma Guest

    "a" <> wrote:

    > Hi,
    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt


    File::Spec

    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    Why? Also, might that File::Spec doesn't make this test necessary.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
    John Bokma, Jan 27, 2006
    #2
    1. Advertising

  3. a

    Guest

    a wrote:
    > I have an input string. It is a full path of a file.
    > How can I parse it into directory and file?


    Hmm. Someone else just asked this same question in a different group...
    the answer is the same; let File::Basename (a built-in Perl module) do
    it for you:

    #!/usr/bin/perl
    use strict; use warnings;

    use File::Basename; # Standard module included in Perl

    my $filename = '//server/dirA/dirB/file.txt';

    my ($name, $path) = fileparse($filename);
    print "$path\n$name\n";

    __END__
    , Jan 27, 2006
    #3
  4. a

    James Taylor Guest

    a <> wrote:

    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt


    If you don't want to use a separate module for such a simple task and
    you know there will be at least one slash directory separator, then a
    simple regex should be good enough.

    my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

    If you can't be sure of getting at least one slash then you could prefix
    one to the $path first, or just check for this situation and assign the
    rest to the $leaf.

    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    Simple:

    if ( $path =~ m|/$| ) {
    # has trailing slash
    } else {
    # no trailing slash
    }

    --
    James Taylor
    James Taylor, Jan 27, 2006
    #4
  5. a wrote:
    > Hi,
    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt
    >
    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    You seem to be very ignorant of anything other people write.
    In your thread "array question", you have been asked to show code that
    you wrote and that dowsn't work. You have been asked to read the posting
    guidelines for this group.

    Yet you post another question showing that you do not want to help us
    help you.

    You're steering straight into a lot of people's killfiles.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Jan 27, 2006
    #5
  6. "a" <> wrote in news:FYiCf.454345$ki.271648@pd7tw2no:

    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt


    This is not just string manipulation. It is path and file name
    manipulation. As such, you'd better served by using File::Spec. Using the
    module would help make your script more portable.

    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    If you use File::Spec->catfile, you need not worry about that.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jan 27, 2006
    #6
  7. a wrote:
    > Hi,
    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt


    As usual: use File::Basename

    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    As usual there are different ways:
    - you could use substr() to extract the last character of the string and eq
    it with '/'
    - you could use m// and anchor the RE to the end of the string
    - ...

    jue
    Jürgen Exner, Jan 27, 2006
    #7
  8. a

    Xicheng Guest

    a wrote:
    > Hi,
    > I have an input string. It is a full path of a file.
    > eg. //server/dirA/dirB/file.txt
    > How can I parse it into directory and file?
    > i.e //server/dirA/dirB/ and file.txt

    unless you have slash '/' in your filename, you can try:

    $str='//server/dirA/dirB/file.txt';
    ($dir,$file)=$str=~m{^(.*?)([^/]*)$};
    print "$dir\n$file"'

    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?

    if $str =~ m{/$} {
    #the last char is /
    }

    Xicheng
    Xicheng, Jan 27, 2006
    #8
  9. a

    Paul Lalli Guest

    Xicheng wrote:
    > a wrote:
    > > Also, I have a directory as an input.
    > > e.g //server/dirA/dirB/
    > > How can I check the last character is a / or not?

    > if $str =~ m{/$} {
    > #the last char is /
    > }


    no need to invoke the regexp engine for such a simple task. (Oh, and
    what you typed is a syntax error).

    if (substr($str, -1) eq '/') {
    print "Last char is a slash\n";
    }

    Paul Lalli
    Paul Lalli, Jan 27, 2006
    #9
  10. a

    Guest

    a <> wrote:
    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?


    perldoc -f substr ... look for the bit about negative
    OFFSETs in the first paragraph.

    Axel
    , Jan 27, 2006
    #10
  11. a

    Xicheng Guest

    Paul Lalli wrote:
    > Xicheng wrote:
    > > a wrote:
    > > > Also, I have a directory as an input.
    > > > e.g //server/dirA/dirB/
    > > > How can I check the last character is a / or not?

    > > if $str =~ m{/$} {
    > > #the last char is /
    > > }

    >
    > no need to invoke the regexp engine for such a simple task. (Oh, and
    > what you typed is a syntax error).

    yep, you are right, I forgot to add parenthesis to the 'if' clause:(

    Xicheng
    > if (substr($str, -1) eq '/') {
    > print "Last char is a slash\n";
    > }
    >
    > Paul Lalli
    Xicheng, Jan 27, 2006
    #11
  12. a

    DJ Stunks Guest

    a wrote:
    > <first question snipped>
    >
    > Also, I have a directory as an input.
    > e.g //server/dirA/dirB/
    > How can I check the last character is a / or not?
    > Thanks a lot


    I assume you only want to check for that trailing slash in order to
    know whether or not it's actually a directory, so just test for that.

    print "$_ is a directory" if ( -d );

    -jp
    DJ Stunks, Jan 27, 2006
    #12
  13. a <> wrote:

    > How can I check the last character is a / or not?



    print "last is a slash\n" if substr($path, -1) eq '/';


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 27, 2006
    #13
  14. James Taylor <> wrote:

    > my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

    ^
    ^

    That anchor serves no useful purpose, so it probably shouldn't be there.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 28, 2006
    #14
  15. a

    James Taylor Guest

    Tad McClellan <> wrote:

    > James Taylor <> wrote:
    >
    > > my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

    > ^
    > ^
    >
    > That anchor serves no useful purpose, so it probably shouldn't be there.


    My newsreader doesn't have a monospaced font (I must fix that) so your
    caret appears to point to the dollar singin front of $path. I must
    assume though that you were pointing at the beginning of line anchor in
    my regex. The reason it is there is to ensure that the match fails in
    linear time if there is no slash in the $path.

    --
    James Taylor
    James Taylor, Jan 28, 2006
    #15
  16. a

    Ch Lamprecht Guest

    James Taylor schrieb:
    > Tad McClellan <> wrote:
    >
    >
    >>James Taylor <> wrote:
    >>
    >>
    >>>my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

    >>
    >> ^
    >> ^
    >>
    >>That anchor serves no useful purpose, so it probably shouldn't be there.

    >
    > The reason it is there is to ensure that the match fails in
    > linear time if there is no slash in the $path.
    >


    However, for strings without slashes, the pattern with the anchor will
    be (a little) slower than without the anchor.

    (If anybody want's to know ;) )


    use warnings;
    use strict;
    use Benchmark qw(cmpthese) ;

    my $string = "a_string_with_or_without_slashes";
    my $string2 = "a_string_with_or/without_slashes";
    cmpthese(1000000, {
    'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
    'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
    'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
    'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


    prints:

    Rate no_an+/ an+/ anchor no_anchor
    no_an+/ 275028/s -- -1% -83% -84%
    an+/ 278164/s 1% -- -83% -84%
    anchor 1636661/s 495% 488% -- -5%
    no_anchor 1721170/s 526% 519% 5% --

    Btw: Is there an expression like 'peacounting' in english ??

    Christoph
    --
    please reply to

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Jan 29, 2006
    #16
  17. a

    Ch Lamprecht Guest

    James Taylor schrieb:
    > Tad McClellan <> wrote:
    >
    >
    >>James Taylor <> wrote:
    >>
    >>
    >>>my ($dir, $leaf) = $path =~ m|^(.*)/(.*)|;

    >>
    >> ^
    >> ^
    >>
    >>That anchor serves no useful purpose, so it probably shouldn't be there.

    >
    > The reason it is there is to ensure that the match fails in
    > linear time if there is no slash in the $path.
    >


    However, for strings without slashes, the pattern with the anchor will
    be (a little) slower than without the anchor.

    (If anybody want's to know ;) )


    use warnings;
    use strict;
    use Benchmark qw(cmpthese) ;

    my $string = "a_string_with_or_without_slashes";
    my $string2 = "a_string_with_or/without_slashes";
    cmpthese(1000000, {
    'anchor' => sub{my($foo,$bar)=$string =~ m|^(.*)/(.*)|},
    'no_anchor' => sub{my($foo,$bar)=$string =~ m|(.*)/(.*)|},
    'an+/' => sub{my($foo,$bar)=$string2 =~ m|^(.*)/(.*)|},
    'no_an+/' => sub{my($foo,$bar)=$string2 =~ m|(.*)/(.*)|} });


    prints:

    Rate no_an+/ an+/ anchor no_anchor
    no_an+/ 275028/s -- -1% -83% -84%
    an+/ 278164/s 1% -- -83% -84%
    anchor 1636661/s 495% 488% -- -5%
    no_anchor 1721170/s 526% 519% 5% --

    Btw: Is there an expression like 'peacounting' in english ??

    Christoph
    --
    please reply to

    perl -e "print scalar reverse q//"
    Ch Lamprecht, Jan 29, 2006
    #17
  18. a

    James Taylor Guest

    Ch Lamprecht <> wrote:

    > Btw: Is there an expression like 'peacounting' in english ??


    Yes, almost the same, it's "bean counting".

    --
    James Taylor
    James Taylor, Jan 29, 2006
    #18
    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. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    800
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,802
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,611
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    806
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    441
    Pete Wright
    May 24, 2004
Loading...

Share This Page