How to remove a variable length of the word from the string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalini jain
    New Member
    • Mar 2007
    • 10

    How to remove a variable length of the word from the string?

    Hi all,

    I dont know much of PERL so i m facing a problem in trying to remove the variable length of word from the string. I have written a code for it. Please guide me whether i will get the desired results or not?
    Let me make you all clear my taking following example:

    $string='//abc1234/default/folder1/images';

    Now i want to remove "//abc1234" from this string. The length of this part of the string may vary that is it may be "//abcd1234" or "abc".
    The key in this string is "default" which will always be there.
    and i also want both the variable part and non variable to be stored in some variable.

    This is the code that i have written:

    [CODE=perl]
    my $string = '//abc1234/default/folder1/images';
    my $check = 'default/';
    my $index = index $string, $check;
    $index = $index - 1;

    my $address = substr $string, 0, $index;
    my $fragment = substr $string, $index;
    [/CODE]

    (I am expecting that $address will give me "//abc1234", the variable part.
    and $fragment will give me "/default/folder1/images").

    Please review the above code and tell me if i m correct or not.
    If i m wrong, then how can i get the desired output? Please help me.
    Last edited by miller; Jun 6 '07, 10:13 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Greetings,

    Your code is correct. However, making some slight modifications gives me this:

    [CODE=perl]
    my $string = '//abc1234/default/folder1/images';
    my $check = '/default';
    my $index = index $string, $check;

    my $address = substr $string, 0, $index;
    my $fragment = substr $string, $index;

    print "$address\n ";
    print "$fragment\ n";
    [/CODE]

    Output is:
    Code:
    >perl scratch.pl
    //abc1234
    /default/folder1/images
    Also, if you want to use a regular expression, this would also work:

    [CODE=perl]
    my $string = '//abc1234/default/folder1/images';
    my $check = '/default';

    $string =~ /(.*?)(\Q$check\ E.*)/;

    my $address = $1;
    my $fragment = $2;
    [/CODE]

    I would suggest that you add error checking to your logic though. It's pretty easy to do in both of these cases.

    - Miller

    Comment

    • shalini jain
      New Member
      • Mar 2007
      • 10

      #3
      Thanks a lot Miller for your reply.

      But i was unable to understand your second point about adding error logic. Will you please elaborate more on this point by giving example?

      I mean are you saying that: I check if I am getting "/default" in my string or not and then apply the above logic..

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        What if there is no such match in your string? In the case of the index function, -1 will be returned. In the case of the regular expression, false will be returned.

        I don't know what your data is like, but it's always safer to add error checking to your parsing just in case your data is not as you expect.

        - Miller

        Comment

        Working...