lafvcu1 has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I've been searching for a very long time how to do the following:
I have an array, containing the elements (for example):
200 201 205 194 140 250 280
I want to substitute all values that are greater than 200 for the number 200, so ideally the new or modified array would contain:
200 200 200 194 140 200 200
My attempt to do this has involved using s///, tr///, etc. etc. Any suggestions?
my code:
@array = ('200', '201', '205', '194', '140', '250', '280'); print "original\n" , "@array " , "\n"; $count = @array; for ($i = 0; $i > $count-1; $i++) { if ($array[$i] gt '200') { s/$array[$i]/200/; } } print "new:\n" , "@array ", "\n";
which returns:
original 200 201 205 194 140 250 280 new 200 201 205 194 140 250 280
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: substituting values in an array
by davido (Cardinal) on Jun 18, 2013 at 16:41 UTC | |
Re: substituting values in an array
by talexb (Chancellor) on Jun 18, 2013 at 16:38 UTC | |
Re: substituting values in an array
by tobyink (Canon) on Jun 18, 2013 at 17:00 UTC | |
by sundialsvc4 (Abbot) on Jun 18, 2013 at 18:02 UTC | |
by tobyink (Canon) on Jun 18, 2013 at 23:01 UTC | |
by Lotus1 (Vicar) on Jun 18, 2013 at 20:25 UTC | |
Re: substituting values in an array
by toolic (Bishop) on Jun 18, 2013 at 16:42 UTC | |
Re: substituting values in an array
by Laurent_R (Canon) on Jun 18, 2013 at 19:01 UTC |
Back to
Seekers of Perl Wisdom