469,626 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. my $string = '//abc1234/default/folder1/images';
  2. my $check = 'default/';
  3. my $index = index $string, $check;
  4. $index = $index - 1;
  5.  
  6. my $address = substr $string, 0, $index; 
  7. my $fragment = substr $string, $index; 
  8.  
(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.
Jun 6 '07 #1
3 1933
miller
1,089 Expert 1GB
Greetings,

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

Expand|Select|Wrap|Line Numbers
  1. my $string = '//abc1234/default/folder1/images';
  2. my $check = '/default';
  3. my $index = index $string, $check;
  4.  
  5. my $address = substr $string, 0, $index;
  6. my $fragment = substr $string, $index;
  7.  
  8. print "$address\n";
  9. print "$fragment\n";
  10.  
Output is:
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. //abc1234
  3. /default/folder1/images
  4.  
Also, if you want to use a regular expression, this would also work:

Expand|Select|Wrap|Line Numbers
  1. my $string = '//abc1234/default/folder1/images';
  2. my $check = '/default';
  3.  
  4. $string =~ /(.*?)(\Q$check\E.*)/;
  5.  
  6. my $address = $1;
  7. my $fragment = $2;
  8.  
I would suggest that you add error checking to your logic though. It's pretty easy to do in both of these cases.

- Miller
Jun 6 '07 #2
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..
Jun 7 '07 #3
miller
1,089 Expert 1GB
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
Jun 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Mikael Petterson | last post: by
12 posts views Thread by Sam Collett | last post: by
15 posts views Thread by morleyc | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.