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: -
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;
-
(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.
3 2130 miller 1,089
Recognized Expert Top Contributor
Greetings,
Your code is correct. However, making some slight modifications gives me this: -
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";
-
Output is: -
>perl scratch.pl
-
//abc1234
-
/default/folder1/images
-
Also, if you want to use a regular expression, this would also work: -
my $string = '//abc1234/default/folder1/images';
-
my $check = '/default';
-
-
$string =~ /(.*?)(\Q$check\E.*)/;
-
-
my $address = $1;
-
my $fragment = $2;
-
I would suggest that you add error checking to your logic though. It's pretty easy to do in both of these cases.
- Miller
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..
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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:
|
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):
|
by: frank |
last post by:
How do I remove the furthest right (last) character in a string in C#?
|
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...
|
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...
| |
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:
|
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
|
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
{
|
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...
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |