split string after third forwardslash

Discussion in 'Perl Misc' started by mike, May 12, 2009.

  1. mike

    mike Guest

    Hi,

    How can I use perl to get the following substring:

    C:\cc_views\wong

    from this string:

    C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

    br,

    //mike
     
    mike, May 12, 2009
    #1
    1. Advertising

  2. mike wrote:
    > Hi,
    >
    > How can I use perl to get the following substring:
    >
    > C:\cc_views\wong
    >
    > from this string:
    >
    > C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl


    What have you tried so far? Where did it fail to meet your expectations?

    There are several ways to do so:
    - use a regular expression (twice a path component followd by a
    backslash, then another path component)
    - use split and then an array slice.

    There are bound to be other methods,

    Josef
    --
    These are my personal views and not those of Fujitsu Technology Solutions!
    Josef Möllers (Pinguinpfleger bei FTS)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://de.ts.fujitsu.com/imprint.html
     
    Josef Moellers, May 12, 2009
    #2
    1. Advertising

  3. mike <> wrote:
    > Hi,
    >
    > How can I use perl to get the following substring:
    >
    > C:\cc_views\wong
    >
    > from this string:
    >
    > C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl



    If the full path is in $full, then use split() along with a "list slice":

    my $partial = join '\\', (split /\\/, $full)[0..2];


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, May 12, 2009
    #3
  4. mike

    ccc31807 Guest

    On May 12, 7:05 am, mike <> wrote:
    > Hi,
    >
    > How can I use perl to get the following substring:
    >
    > C:\cc_views\wong
    >
    > from this string:
    >
    > C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl


    In most situations where you need to separate a string with values
    delimited by some character(s), you can split the string into an array
    variable by using split(), like:

    $string = 'C:\cc_views\wong\mbv_admin\tools\scripts\deliver
    \delivery.pl';
    my @array = split(/\\/, $string);

    This gives @array values like:
    $array[0] = C:
    $array[1] = cc_views
    $array[2] = wong

    And so on.

    You can create a string using several methods, one of which is sprintf
    (), like this:
    $target = sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);

    This isn't the most concise way, but it's easy and understandable.
    CC
     
    ccc31807, May 12, 2009
    #4
  5. ccc31807 <> wrote:
    >$target = sprintf("%s\%s\%s",$array[0],$array[1],$array[2]);


    Ouch, this hurts. Are you programming Perl or C?

    >This isn't the most concise way, but it's easy and understandable.


    Well, why bother with sprintf() and its antics? IMO a good old plain
    $target = "$array[0]\\$array[1]\\$array[2]";
    is way easier and more understandable.

    Of course a
    $target = join '\', @array[0..2];
    is probably more perlish.

    jue
     
    Jürgen Exner, May 12, 2009
    #5
  6. mike

    ccc31807 Guest

    On May 12, 11:14 am, Jürgen Exner <> wrote:
    > Ouch, this hurts. Are you programming Perl or C?


    Personally, I favor sprintf.

    If you want to compare it to something that REALLY causes pain, look
    at Lisp's format function:
    (format t "~{~{~a:~10t~a~%~}~%~}" *db*))

    > Well, why bother with sprintf() and its antics? IMO a good old plain
    >         $target = "$array[0]\\$array[1]\\$array[2]";
    > is way easier and more understandable.


    or even $target = $array[0].'\'.$array[1].'\'.$array[2]

    Does the solidus escape the following character with single quotes?

    CC
     
    ccc31807, May 12, 2009
    #6
  7. mike

    ccc31807 Guest

    On May 12, 4:17 pm, Ben Morrow <> wrote:
    > What happened when you tried it?


    I didn't try it, I just threw it out there. I'm just killing time
    between a series of DB queries at work and one just popped so I hit
    'Send.' Obviously, I'd find out what happened if I needed to write it,
    but since I didn't I didn't.

    > (It escapes the delimiter or backslash itself. Other \X sequences are
    > passed through unchanged.)


    Yeah, seems that I remember that now.

    CC
     
    ccc31807, May 12, 2009
    #7
  8. mike

    Dr.Ruud Guest

    Tad J McClellan wrote:
    > mike <> wrote:
    >> Hi,
    >>
    >> How can I use perl to get the following substring:
    >>
    >> C:\cc_views\wong
    >>
    >> from this string:
    >>
    >> C:\cc_views\wong\mbv_admin\tools\scripts\deliver\delivery.pl

    >
    >
    > If the full path is in $full, then use split() along with a "list slice":
    >
    > my $partial = join '\\', (split /\\/, $full)[0..2];


    There is no need to repeat the split string:

    my $partial = join "", (split /(\\)/, $full)[0..2*2];

    --
    Ruud
     
    Dr.Ruud, May 13, 2009
    #8
    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. Replies:
    2
    Views:
    474
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    712
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    224
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    254
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    636
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page