473,436 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,436 software developers and data experts.

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 2114
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

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

Similar topics

4
by: Mikael Petterson | last post by:
Hi, I got some help to go from: bbBusState to BB_BUS_STATE. However I found out :-( that the xml contained attributes like: TxDeviceGroup and that becomes:
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
4
by: frank | last post by:
How do I remove the furthest right (last) character in a string in C#?
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
5
by: xirowei | last post by:
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable? below is my code: import javax.servlet.*; import javax.servlet.http.*; import...
2
by: slimewizard | last post by:
Hello there! I'm writing a 'hang man' program as a bit of a practice exercise and I've ran into a bit of a problem; I just need a little advise if anyone would be so kind. My apologies if this...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.