Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: substr and strings on the outside

by JPaul (Hermit)
on Aug 20, 2001 at 02:10 UTC ( [id://106090]=note: print w/replies, xml ) Need Help??


in reply to substr and strings on the outside

My apologies for being vague,
Imagine a billing statement in text form. There are a number of lines, generally all different lengths before the \n, which I need to alter.
For example, a line may be (literally):
Joe Bloggs, 1615 Thislane Street\n
Now imagine I need to replace the address with a piece of text that is longer than the one in the original text - thusly my
substr($f, 20, 30, "This is a new longer address\n");
will have substr complain 'substr outside string'.
What I want perl to do is say "Okay, its longer than the original string was, thats fine, I don't really care. Honest."

Hopefully this is clearer

JP
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Replies are listed 'Best First'.
Re: Re: substr and strings on the outside
by scott (Chaplain) on Aug 20, 2001 at 02:45 UTC

    This works fine for me:

    use strict; use warnings; my $f= " Joe Bloggs, 1615 Thislane Street\n"; substr($f, 20, 12345, "This is a new longer address\n"); print $f;

    substr has no problems making the string longer. It's inserting new bytes where the first new byte is beyond the end of the original scalar that causes errors. So the following fails with the error message you mention:

    substr($f, length($f) + 10, 12345, "This is a new longer address\n");

Re: substr and strings on the outside
by mandog (Curate) on Aug 20, 2001 at 07:05 UTC
    I suspect I don't understand the question (or Perl) but...

    If you are trying to insert an arbitary string wouldn't s/// do the trick?

    use strict; use warnings; my $f="Joe Bloggs, 1615 Thislane Street, MyTown, NZ, 1851\n"; my $in=" This is a new longer address"; print $f; $f=~s/,.*/,$in/s; print $f;
    --mandog

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://106090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found