Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Substr Problem

by snape (Pilgrim)
on Sep 27, 2012 at 00:42 UTC ( #995890=perlquestion: print w/replies, xml ) Need Help??
snape has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a string of 0s and I am trying to convert 0s to numerical values to that given index and offset. for example:

my $VALUE = "0\t" x 10; ## array of 0s and I having \t because I want +a delimited outputfile chop($VALUE); print "First statement: ,$VALUE,"\n"; my $signal = "0.5\t0.845"; substr($VALUE, 2,2) =~ s/0/$signal/g; ## want to make changes at index + 2 and 3 print "Second statement: ",$VALUE,"\n";
******Results:******* First statement: 0 0 0 0 0 0 0 0 0 0 Second statement: 0 0.5 0.845 0 0 0 0 0 0 0 + 0

I can understand how perl is thinking about it. As it is making the changes at index 2 and not 3. Therefore, I am getting a vector of 11 numbers rather than 10. One way could be using for loop and then making the change but I would like to know if there is a shorter way to do it rather than using loops. Thanks.

Replies are listed 'Best First'.
Re: Substr Problem
by GrandFather (Sage) on Sep 27, 2012 at 01:38 UTC

    It's not over clear what you want to do, but my best guess is that you have a vector of data where you want to alter some of the values then print the resulting vector. A good way to do that is to transform your string representation of the data to an array, edit the data in the array, then write it back out as a string:

    use strict; use warnings; use 5.010; my $str = "0\t" x 10; my @values = split '\s+', $str; printf "First statement:\t%s\n", join "\t", @values; @values[2, 3] = (0.5, 0.845); printf "Second statement:\t%s\n", join "\t", @values;


    First statement: 0 0 0 0 0 0 0 0 0 0 Second statement: 0 0 0.5 0.845 0 0 0 0 0 + 0
    True laziness is hard work

      Thanks a lot. The mistake I was doing was my thinking in the substr way ro solve my problem.

Re: Substr Problem
by Athanasius (Chancellor) on Sep 27, 2012 at 02:19 UTC

    Hello snape,

    GrandFather’s solution is definitely the way to go. However, for the record, your code can be made to give the output you want by changing the substitution:

    substr($VALUE, 2, 2) =~ s/0/$signal/g;

    into a simple assignment:

    substr($VALUE, 2, 3) = $signal;

    By the way, the /g modifier on the substitution is redundant in this case.

    Athanasius <°(((><contra mundum

      Or, even simpler:
      substr $VALUE, 2, 3, $signal;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995890]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2017-03-27 02:14 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.