Do you know where your variables are? PerlMonks

### Substr Problem

by snape (Pilgrim)
 on Sep 27, 2012 at 00:42 UTC 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;

Prints:

```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;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlquestion [id://995890]
Approved by GrandFather
help
Chatterbox?
 [Happy-the-monk]: holli: Heizungszü ndungselektrik? [holli]: I just ordered a pair of deans connectors to replace them. probably propriatary

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-11-19 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (281 votes). Check out past polls.

Notices?