0

I've recently been working on a string problem in perl and I can't get my hear around one specific part>

Let's say we have a string $str = "tltltltltltltlttl"; There is a specific region on the string, which interests us, let's say from 10th char to 15th char. we run the string through a program, which gives us a 2d conformation of this string example> $str2 =

<pre>
 tl      t    
   tltltl tl
            t
            tl
tltltlt tltl 
       l    tl 
</pre>

I am perfectly aware how one is supposed to find the wanted region in the $str (just use substr $str, 10, 6. Let's say we save this region as $region. The question is. How do I find that exact region in the $str2, with the locations of chars same as in the first string, example>

<pre>
$str =tltltl; $region = substr $str,2,5
$str2 =
tl
  t (this is the first char I want located in $str2)
  l (this is the second char I want located in $str2)
t
 l

</pre>

I cannot seem to somehow map the regions from the first one to some code that would explicitly tell me where are related regions in $str2. SO basically I need to somehow maintain the index through the transformation to $str2 process. Thanks.

2
  • What is the expected result?
    – Toto
    Dec 19, 2014 at 9:05
  • Just edited the question, look there. (result is region from $str located in $str2 somehow.) Dec 19, 2014 at 17:08

2 Answers 2

1

I believe what you are looking for is the index function

my $str = "tltltl";
my $region = substr($str,3,3); #ltl
my $str2 = "tlltl";
my $idx = index("$str2", "$region");
#idx is now 2 which is the starting position of ltl in tlltl
5
  • This works well for a 1d string but when trying to locate that same region from 1d in 2d, it doesn't work. (or I just don't see it). The point is, the indexes of characters in 2d are different than those in 1d Dec 18, 2014 at 21:41
  • I added the smg flags to handle your cases Dec 18, 2014 at 21:47
  • So what i'm basically trying to accomplish is keep the indexes from the string in the 2d string structure. I don't think this code does this (correct me if I'm wrong). I'm sorry if I didn't present the problem in the right way. Dec 19, 2014 at 16:44
  • Maybe rephrase your question I'm not understanding what you want. Dec 19, 2014 at 16:51
  • Changed my answer @user3523464 Dec 19, 2014 at 18:03
1

You want to use substr to accomplish this. There are lots of examples of the most basic cases here to get you started: http://perldoc.perl.org/functions/substr.html

In the example below the first number (10) indicates the char offset (how far from the beginning you want to start), and the second number (4) indicates the length of your new substring.

EXAMPLE:

$string = "tltltltltltltlt";

$specificRegion = substr $string, 10, 4;

print "This is the region we care about: $specificRegion";

RESULT:
tltl

Now you have the variable $specificRegion available for you to use which is the region you wanted to capture for manipulation

4
  • You didn't quite understand the problem. When I want to find that specific region in my 2d string structure, it finds the correct onke and many others which are not correct. Substring I assume must be at least a part of solution it think tho. Dec 18, 2014 at 21:37
  • You said "The question is how does one know where is the specific region marked in the first step, i.e. where is the region which we want to substitute." My answer was meant to answer that by finding the specific region (chars 10-13) in the original string as I thought you were asking. Can you please clarify what you are looking for and what it is you expect as a result?
    – laurenOlga
    Dec 19, 2014 at 16:45
  • THe problem with your code is the following> when you I try to find that $specificregion in my 2d structure ($str2), what happens is perl finds A:: the $specificregion sequences within it, yet I want only the one from the $str. The reason for many results are numberous repetitions of chars in this problem. Dec 19, 2014 at 18:26
  • 1
    Personally I think this is a communication not a code problem lol. My code does what I wanted... but following your edits and comments I see it wasn't what you wanted. If you want returned the position of related segments found in your $str2 then following the use of substr (in the way outlined above) I would recommend as @ZachLeighton mentions below making use of the index funtion.
    – laurenOlga
    Dec 19, 2014 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.