Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

substr and strings on the outside

by JPaul (Hermit)
on Aug 19, 2001 at 19:45 UTC ( #106032=perlquestion: print w/replies, xml ) Need Help??
JPaul has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all,

I'm doing a fair amount of work on editing strings inside a human-readable statement file, and have found using substr has its limits.
Take this:

my $f = " "; substr($f, 5, 5, "12345");
Which will, quickly, complain of 'substr outside string'. Admittedly, I understand _why_ perl is doing it - but I'd rather perl just stuck in a few leading " "s and not complain at me about it.
Any thoughts on a way to do this _without_ initialising all the strings to 80 chars w/padded spaces - and then for the sake of filesize, removing all trailing spaces after text and before the newline marker?

Cheers,
JP

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

Replies are listed 'Best First'.
Re: substr and strings on the outside
by OeufMayo (Curate) on Aug 19, 2001 at 20:23 UTC

    Hello,
    I'm not sure I perfectly understood what you wanted to do. Do you want to append '12345' to the $f variable, with at least 5 spaces at the beginning?

    If so, here's a snippet that should do what you want:

    sub pad { my ($spaces, $string) = @_; return( " " x (5 - length $spaces) . $string ); } my $add = "12345"; my $f = " " x 4; # for leading spaces smaller than 5 print "[" . pad( $f, '12345') . "]";
    <kbd>--
    my $OeufMayo = new PerlMonger::Paris({http => 'paris.mongueurs.net'});</kbd>
Re: substr and strings on the outside
by George_Sherston (Vicar) on Aug 19, 2001 at 20:29 UTC
    I'm not sure this does what you want in all cases, but it might be a step forward:
    my $f = " "; substr($f, length $f, length $f, "12345");
    Or do you want the new string to go in at exactly position 5, even if the old string is shorter than that? Then:
    my $f = " "; my $l = length $f; substr($f, $l, $l, " " x ($l<5 ? 5-$l : 0) . "12345");
    Having said that, this all may betray my failure to understand the question, since one could get the same result with
    $f . " " x ($l<5 ? 5-$l : 0) . "12345".

    George Sherston
(elbie): substr and strings on the outside
by elbie (Curate) on Aug 19, 2001 at 20:20 UTC
    Regex search and replace:

    my $f = " "; $f =~ s/^(.{0,5}).{0,5}/${1}12345/;

    The only problem is this regex doesn't pad extra spaces if your string is too short, but at least it still does the replacement.

    elbieelbieelbie

Re: substr and strings on the outside
by synapse0 (Pilgrim) on Aug 20, 2001 at 01:52 UTC
    I'm not sure I fully understand what you are trying to do.. but maybe sprintf might help you.
    -Syn0
Re: substr and strings on the outside
by JPaul (Hermit) on Aug 20, 2001 at 02:10 UTC
    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 --

      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");

      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
Node Status?
node history
Node Type: perlquestion [id://106032]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-01-18 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (206 votes). Check out past polls.

    Notices?