473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

10 New Member
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 2130
miller
1,089 Recognized Expert Top Contributor
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
shalini jain
10 New Member
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 Recognized Expert Top Contributor
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
2998
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
55532
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 also removed) So far I have (val is value, ar is array, returns new array):
4
14796
by: frank | last post by:
How do I remove the furthest right (last) character in a string in C#?
11
5813
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 using the split function with ' ', but how do i get it to take into account punctuation marks such as ',',',' etc? Also I am then trying to add...
15
16307
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 to end at the end of a word, if you catch my drift. For example, I have a large string variable containing the text: "I've seen things you people...
3
5382
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 example first:
15
50166
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
6051
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 java.io.*; public class LabWork3_Q1 extends HttpServlet {
2
3192
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 has been covered here before; I've tried googling this but I don't know how to word it so my results have been quite unhelpful. I'll try my best to...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.