473,386 Members | 1,812 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,386 software developers and data experts.

Substring doubt?

89
Hi

Please let me know any fnction to do this. I have started learning perl and any guidance would be useful.

Suppose I have a string as below;
Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
Suppose if I want to look for a word
Expand|Select|Wrap|Line Numbers
  1. time
, I can use a regex to find whether he string contains the word time or not.

But I need a way to chop the original string and count the number of characters in that string.

In tha above example
Expand|Select|Wrap|Line Numbers
  1. Nowisthe
is the one I am interested. string before time is
Expand|Select|Wrap|Line Numbers
  1. Nowisthe
and the length of the string is 8. How do I do it programatically? Thanks.
Nov 4 '08 #1
4 1429
nithinpes
410 Expert 256MB
If you are trying to capture the string preceeding the match, you can make use of the special variable $`. e.g
Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheir  party'; 
  2.  
  3. if($string=~/time/) {
  4. my $pre = $`; # get characters preceeding the match
  5. my $length=length($pre);
  6. print "$pre  : ($length)\n"
  7. }
  8.  
This will produce:
Expand|Select|Wrap|Line Numbers
  1. Nowisthe  : (8)
  2.  
Similarly, $' can be used to capture string succeeding the match.
Nov 4 '08 #2
KevinADC
4,059 Expert 2GB
If you are really wanting to find where a substring is in a string as versus finding a pattern, the index function is made for that:

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletocometotheaidoftheirparty';
  2. my $where = index $string, 'time';
  3. print $where;
The suggestion nithinpes has posted is an easy one to implement as it takes advantage of some of perls builtin variables and is easy to stick into a script and getting working. It unfortunately inccurs a considerable performance penalty on any other regexps used in the code. An alternative is to use another perl builtin that does not introduce the performance penalty: @-

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
  2. if($string=~/time/) {
  3.    print $-[0]; # get the offset
  4. }
Nov 4 '08 #3
nithinpes
410 Expert 256MB
The suggestion nithinpes has posted is an easy one to implement as it takes advantage of some of perls builtin variables and is easy to stick into a script and getting working. It unfortunately inccurs a considerable performance penalty on any other regexps used in the code. An alternative is to use another perl builtin that does not introduce the performance penalty: @-

Expand|Select|Wrap|Line Numbers
  1. my $string = 'Nowisthetimeforallgoodpeopletimetocometotheaidoftheirparty'; 
  2. if($string=~/time/) {
  3.    print $-[0]; # get the offset
  4. }
That's cool :) . I never knew of @-.
Nov 5 '08 #4
KevinADC
4,059 Expert 2GB
Theres also @+. You can read about them in perlvar
Nov 5 '08 #5

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

Similar topics

7
by: cpp_weenie | last post by:
Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters...
7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
7
by: Chris Lasher | last post by:
Hi all, How can one count all the permutations of a substring in a string? For a more concrete example, let's say targetstr = 'AAA' and probestr = 'AA' I want to consider how many times one...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
1
by: sandhya2006 | last post by:
Dear Friends, I have two textfiles , Ex:first text is like this AAA A B C D .................. .................... ZZZ CC GG Second text file is A a B b
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
53
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.