For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > substr









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author substr
mk76@buffalo.edu

2004-10-31, 3:55 am

Hi All,

I need to copy a substring in a string which is completely out of the
primary string (in terms of the offset). substr does not allow me to do
so. Is there any other way in which I can do that ?

Thanks,
Manas.
John W. Krahn

2004-10-31, 3:55 am

mk76@buffalo.edu wrote:
> Hi All,


Hello,

> I need to copy a substring in a string which is completely out of the
> primary string (in terms of the offset). substr does not allow me to do
> so.


Do you mean like this:

$ perl -le' $_ = q/abc/; substr( $_, 20 ) = q/def/; print'
substr outside of string at -e line 1.


> Is there any other way in which I can do that ?


What do you want to use to fill in the string between the two?
If you want to do what I think you want to do then you could use pack:

$ perl -le' $_ = q/abc/; $_ = pack q/A20 A*/, $_, q/def/; print'
abc def

Or sprintf:

$ perl -le' $_ = q/abc/; $_ = sprintf q/%-20s%s/, $_, q/def/; print'
abc def



John
--
use Perl;
program
fulfillment
mk76@buffalo.edu

2004-10-31, 3:55 am

My earlier post was not quite clear. I will explain my problem in
detail here.

I have a string whose length is say 5. I need to copy another string
into this string but from an offset of 10.
I tried using substr but this is a limitation of substr and it gave me
a fatal error.
Is there any other function, method which will allow me to do so ?

Manas.


Quoting mk76@buffalo.edu:

> Hi All,
>
> I need to copy a substring in a string which is completely out of
> the
> primary string (in terms of the offset). substr does not allow me to
> do
> so. Is there any other way in which I can do that ?
>
> Thanks,
> Manas.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
>

Chasecreek Systemhouse

2004-10-31, 3:55 am

On Sat, 30 Oct 2004 22:46:39 -0400, mk76@buffalo.edu <mk76@buffalo.edu> wrote:
> My earlier post was not quite clear. I will explain my problem in
> detail here.
>
> I have a string whose length is say 5. I need to copy another string
> into this string but from an offset of 10.
> I tried using substr but this is a limitation of substr and it gave me
> a fatal error.
> Is there any other function, method which will allow me to do so ?
>
> Manas.



Yes, "rindex" is what (I think) you want; here is a small example of its use:

my($newString) = substr($currentString, (rindex($currentString,
"searchString") + length("searchString".$replacementString)));


Although, John's reply to you first post was better.

--
WC -Sx- Jones
http://youve-reached-the.endoftheinternet.org/
Robert Citek

2004-10-31, 3:55 pm


On Saturday, Oct 30, 2004, at 21:46 US/Central, mk76@buffalo.edu wrote:
> My earlier post was not quite clear. I will explain my problem in
> detail here.
>
> I have a string whose length is say 5. I need to copy another string
> into this string but from an offset of 10.
> I tried using substr but this is a limitation of substr and it gave me
> a fatal error.
> Is there any other function, method which will allow me to do so ?


Sounds like what you want to do is concatenate two strings with
something in between. For example:

$ perl -e '
$foo="abcde";
$bar="fghij";
$foobar=$foo . " " . $bar;
print "$foo : $bar : $foobar\n";
'
abcde : fghij : abcde fghij

Is that close to what you are looking for? If not, could you post some
sample code of what you are trying to do along with the error message,
sample input data, and the expected output data? I find it easier to
debug buggy code than to debug descriptions of buggy code.

Regards,
- Robert

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com