in reply to Re^2: Changing the numerical value from 0 to 1
in thread Changing the numerical value from 0 to 1

You'll need to show the data in order for us to provide a meaningful response.

If you actually print your values, you may be able to see why the substitution isn't working for yourself.

-- Ken

  • Comment on Re^3: Changing the numerical value from 0 to 1

Replies are listed 'Best First'.
Re^4: Changing the numerical value from 0 to 1
by hellohello1 (Sexton) on Jan 28, 2014 at 06:30 UTC
    Hi Kcott, I think it works this time. I put it at @resultarray instead of @output5b.  for (@resultarray) { s/^0$/1/ }; So far, it seems to work ok, though I am trying to figure out why putting at @output5b doesn't work since I am printing the values from @output5b. Here's my whole code
    for (@resultarray) { s/^0$/1/ }; for(my $i = 0; $i < $originalfilecount; $i++) { if($first) { $first = 0; #print OUT5 "\t$resultarray[3]"; $outputb = "\t$resultarray[3]"; } } #print OUT5 "\n"; push(@output5a, $outputa); push(@output5b, $outputb); for(@output5b){s/0/1/g}; #convert output from 0 to 1
    May I ask how do I substitute the $resultarray3 values from 0 to 1 instead of doing at @resultarray? Thanks for all your help :)
      ... how do I substitute the $resultarray[3] values from 0 to 1 instead of doing at @resultarray?

      One way:

      >perl -wMstrict -le "use Data::Dump; ;; my @resultarray = (0, 0, 0, 0, 0, 0); dd \@resultarray; ;; $resultarray[3] =~ s/^0$/1/; dd \@resultarray; " [0, 0, 0, 0, 0, 0] [0, 0, 0, 1, 0, 0]

      Your problem is almost certainly this line:

      $outputb = "\t$resultarray[3]";

      $outputb does not equal $resultarray[3]; it equals a TAB ('\t') followed by $resultarray[3].

      Here's some examples of the types of tests you could have run to check this yourself:

      #!/usr/bin/env perl -l use strict; use warnings; my @resultarray; my $outputb; $resultarray[3] = 0; $outputb = "\t$resultarray[3]"; print '$resultarray[3] = |', $resultarray[3], '|'; print '$outputb = |', $outputb, '|'; check_for_match_zero('$resultarray[3]', $resultarray[3]); check_for_match_zero('$outputb', $outputb); sub check_for_match_zero { my ($name, $value) = @_; if ($value =~ /^0$/) { print "$name (with value: '$value') MATCHES zero."; } else { print "$name (with value: '$value') DOES NOT MATCH zero."; } }

      Output:

      $resultarray[3] = |0| $outputb = | 0| $resultarray[3] (with value: '0') MATCHES zero. $outputb (with value: ' 0') DOES NOT MATCH zero.

      [I see ++AnomalousMonk has shown you how to do the substitution on just one element (i.e. $resultarray[3]) as requested.]

      -- Ken