On 17/03/2011 20:16, Chris Stinemetz wrote:
I am trying to return new values based on if else. I understand the idea of using if else, but I am not sure I have placed in the right place. I was assuming I would want to insert it before the array is sorted. Thank you in advance. This mailing list is helping me understand perl greatly! I am getting the following error: Use of uninitialized value within @data in pattern match (m//) at ./DOband.pl line 19,<$fh> line 485. > #!/usr/bin/perl use warnings; use strict; my $filepath = 'C:/temp/PCMD'; my $outfile = 'output.txt'; open my $fh, '<', $filepath or die "ERROR opening $filepath: $!"; open my $out, '>', $outfile or die "ERROR opening $outfile: $!"; my @array; while (<$fh>) { next unless /;/; chomp; my @data = ( split /;/ )[31,32,38,39,261]; if (@data[39] =~ /15/) { 2 } else { 1 } push @array, join "\t", @data; }
split /;/ is splits $_ into a list of many (hopefully at least 262) fields. This is indexed with [31,32,38,39,261], which selects a 'slice' of five elements and assigns them to the array @data. These are now accessible as $data[0] through $data[4]. So I presume that by @data[39] you mean $data[3]? It is unclear what you want the if statement to do; perhaps change the value of this element if it happens to be 15? If you are comparing numbers then it is wrong to use regular expressions (/15/ will match anything containing those two characters, like 'XXX15XXX'). A simple == comparison will suffice. So to change the fourth selected field to 1, or to 2 if it happens to be 15, you would write if ($data[3] == 15) { $data[3] = 2; } else { $data[3] = 1; } I hope that is a satisfactory answer. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/