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

regular expessions question: (replacing words)

by $new_guy (Acolyte)
on Sep 27, 2010 at 11:51 UTC ( #862193=perlquestion: print w/ replies, xml ) Need Help??
$new_guy has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks,

Hello. I have a file "organized.txt". I would like to replace all but the first element in each line of the file with a "z". The file organized is attached - below!

I have written a script, below, but it only prints out the first element and not the other elements meant to be z's. Please tell me where I am going wrong with the script.

thanks

##my script
#!usr/bin/perl my $FILENAME4 = "organized.txt"; open(DATA2, $FILENAME4); #remove all previous re-organized files my $remove_reorganized = "re-organized.txt"; if (unlink($remove_reorganized) == 1) { print "Existing \"re-organized.txt\" file was +removed\n"; } #now make a file for the ouput my $outputfile = "re-organized.txt"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n" +; exit; } while (my $organized = <DATA2>) { #do some re-organizing #sort out the group numbers first my ($first, $second) = split(/^\s{1}/, $organized); $first =~ /(\d+){1}/; my $first_num = $1; #now sort out the other data #create an array to hold the data my @storage = (); my @final_storage = (); my @second = split(/\s+/, $second); foreach my $second_num(@second){ $second_num =~ s/(\w+)/z/g; @storage = $second_num; push (@storage, $1); } push @final_storage, $storage[$_] foreach 0..$#storage; print POS "$first_num @final_storage\n"; }
## my data file - organized
0 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + - 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 0 SPN23F SPCG SPJ spr SPP SPH + - HMPREF0837 - SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 - 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 - 3850_2_8 3850_1_1 - + - 3850_2_7 - 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 - 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 - 3850_7_12 3850_1_ +9 3850_7_6 - 3850_5_10 3850_6_6 3850_2_2 - + - 3850_5_3 - 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 - 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 - 385 +0_2_9 3850_8_6 - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 0 SPN23F SPCG SPJ spr SPP SPH + - HMPREF0837 - SPG SP70585 SPT 385 +0_8_3 - 3850_7_11 - - 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 - 3850_2_8 - - + - 3850_2_7 - - - 3850_5_5 + - - 3850_6_10 3850_1_8 3850_3_1 3850_6_2 + - 3850_6_12 3850_3_9 3850_1_3 - 3850_7_12 3850_1_ +9 3850_7_6 - - - 3850_2_2 - + - 3850_5_3 - - 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 - 3850_2_1 - - 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 - 3850_5_4 + - 3850_2_6 3850_5_8 3850_3_10 - - + - 3850_3_2 3850_3_12 - - - 385 +0_2_9 3850_8_6 - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 - 3850_7_7 3850_5_2 + 3850_7_3 - 0 SPN23F SPCG SPJ spr SPP SPH + - HMPREF0837 - SPG SP70585 SPT 385 +0_8_3 - - - - 3850_2_10 + - 3850_8_8 3850_1_12 - 3850_2_8 - - + - 3850_2_7 - - - 3850_5_5 + - - 3850_6_10 3850_1_8 3850_3_1 3850_6_2 + - 3850_6_12 - 3850_1_3 - 3850_7_12 3850_1_ +9 - - - - - - + - 3850_5_3 - - 3850_8_9 - + - 3850_7_8 - 3850_2_1 - - 3850_3 +_11 - 3850_3_7 - - - - + - - 3850_5_8 3850_3_10 - - + - 3850_3_2 3850_3_12 - - - 385 +0_2_9 3850_8_6 - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 - 3850_2_11 - - - - + 3850_7_3 - 0 SPN23F SPCG - spr - SPH + - HMPREF0837 - SPG SP70585 - + - - - - - 3850_2_10 + - - - - - - - + - - - - - 3850_5_5 + - - - 3850_1_8 3850_3_1 - + - - - - - - +- - - - - - - + - - - - 3850_8_9 - + - - - 3850_2_1 - - 3850_3 +_11 - - - - - - + - - - - - - + - 3850_3_2 - - - - 385 +0_2_9 - - 3850_3_4 - 3850_2_4 3850_2_ +12 - 3850_2_11 - - - - + - - 0 - SPCG - spr - - + - HMPREF0837 - SPG - - + - - - - - - + - - - - - - - + - - - - - - + - - - 3850_1_8 3850_3_1 - + - - - - - - +- - - - - - - + - - - - - - + - - - 3850_2_1 - - + - - - - - - - + - - - - - - + - 3850_3_2 - - - - 385 +0_2_9 - - 3850_3_4 - - 3850_2_ +12 - 3850_2_11 - - - - + - - 0 - SPCG - spr - - + - - - SPG - - + - - - - - - + - - - - - - - + - - - - - - + - - - - 3850_3_1 - + - - - - - - +- - - - - - - + - - - - - - + - - - 3850_2_1 - - + - - - - - - - + - - - - - - + - - - - - - 385 +0_2_9 - - 3850_3_4 - - + - - 3850_2_11 - - - - + - - 0 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - 3850_2_1 - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 0 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - 3850_2_1 - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 - 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 + - 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 - + 3850_6_4 3850_5_3 3850_5_1 - 3850_8_9 3850_3_8 + - 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 - 3850_5_2 + 3850_7_3 3850_1_7 1 SPN23F SPCG SPJ spr - SPH + - HMPREF0837 SP SPG SP70585 - 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 - 3850_2_10 3850_5_ +12 3850_8_8 - - 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 - - - 3 +850_7_2 - - 3850_1_8 3850_3_1 3850_6_2 + - 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 - 3850_5_10 3850_6_6 3850_2_2 - + - 3850_5_3 - - 3850_8_9 3850_3_8 + - 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 - - 3850_5_4 + - 3850_2_6 3850_5_8 3850_3_10 3850_7_10 - + - 3850_3_2 - 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 - - + 3850_7_3 3850_1_7 1 SPN23F SPCG SPJ spr - SPH + - HMPREF0837 SP SPG SP70585 - + - 3850_8_2 - 3850_8_7 - 3850_2_10 3850_5_ +12 - - - 3850_2_8 - - + - 3850_2_7 3850_6_7 - - - + - - - - 3850_3_1 3850_6_2 + - 3850_6_12 3850_3_9 - - 3850_7_12 3850_1_ +9 - - 3850_5_10 - 3850_2_2 - + - - - - 3850_8_9 3850_3_8 + - - - - - - 3850_3 +_11 - 3850_3_7 3850_2_3 - - 3850_5_4 + - 3850_2_6 3850_5_8 3850_3_10 - - + - 3850_3_2 - - 3850_8_12 - 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 - 3850_2_11 - - - - + - - 1 SPN23F SPCG SPJ spr - SPH + - HMPREF0837 SP SPG SP70585 - + - 3850_8_2 - - - 3850_2_10 + - - - - - - - + - - 3850_6_7 - - - + - - - - 3850_3_1 3850_6_2 + - 3850_6_12 3850_3_9 - - - +- - - 3850_5_10 - 3850_2_2 - + - - - - - 3850_3_8 + - - - - - - 3850_3 +_11 - - 3850_2_3 - - - + - - - 3850_3_10 - - + - 3850_3_2 - - - - 385 +0_2_9 - - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 - 3850_2_11 - - - - + - - 1 SPN23F SPCG SPJ spr - SPH + - HMPREF0837 SP SPG SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - 3850_6_12 - - - - +- - - 3850_5_10 - 3850_2_2 - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - 3850_3_4 - 3850_2_4 + - - 3850_2_11 - - - - + - - 1 SPN23F SPCG SPJ spr - SPH + - HMPREF0837 SP SPG SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - 3850_6_12 - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG SPJ spr - - + - HMPREF0837 SP SPG SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG SPJ spr - - + - HMPREF0837 SP SPG SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG SPJ spr - - + - HMPREF0837 - SPG SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG SPJ spr - - + - HMPREF0837 - - SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - spr - - + - HMPREF0837 - - SP70585 - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - HMPREF0837 - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - HMPREF0837 - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 SPN23F SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 - SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 - SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 1 - SPCG - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 2 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 2 SPN23F - SPJ - SPP SPH + - - - - SP70585 - 385 +0_8_3 - - 3850_8_7 3850_1_6 3850_2_10 + - - 3850_1_12 - - 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 - 3850_5_9 - 3850_5_5 3 +850_7_2 - 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 - 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 - 3850_8_5 3850_3 +_11 3850_2_5 - 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 - 3850_3_10 3850_7_10 3850_3_6 + - - 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 - - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 - - 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 2 SPN23F - SPJ - - - + - - - - - - 385 +0_8_3 - - 3850_8_7 3850_1_6 - + - - 3850_1_12 - - 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 - - - 3850_5_5 3 +850_7_2 - 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 - 3850_6_8 3850_7_12 3850_1_ +9 - 3850_6_5 3850_5_10 - 3850_2_2 3850_1_2 + - 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 + - 3850_7_8 3850_3_3 3850_2_1 - 3850_8_5 + - - - 3850_2_3 3850_8_11 3850_7_5 - + 3850_6_1 3850_2_6 - 3850_3_10 3850_7_10 3850_3_6 + - - 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 - - 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 - - 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 2 - - - - - - + - - - - - - 385 +0_8_3 - - 3850_8_7 3850_1_6 - + - - 3850_1_12 - - - - + - - - - - - + - - 3850_6_10 - 3850_3_1 - 3850 +_5_7 3850_6_12 3850_3_9 - 3850_6_8 3850_7_12 3850_1_ +9 - - 3850_5_10 - 3850_2_2 3850_1_2 + - 3850_5_3 - 3850_6_3 - 3850_3_8 + - 3850_7_8 3850_3_3 3850_2_1 - - + - - - 3850_2_3 3850_8_11 3850_7_5 - + - - - 3850_3_10 3850_7_10 3850_3_6 + - - 3850_3_12 3850_8_4 - 3850_8_1 + - - - - 3850_1_10 3850_2_4 3850_2_ +12 - - 3850_7_1 - - - + 3850_7_3 3850_1_7 2 - - - - - - + - - - - - - + - - - - 3850_1_6 - + - - 3850_1_12 - - - - + - - - - - - + - - 3850_6_10 - - - 3850 +_5_7 - 3850_3_9 - 3850_6_8 3850_7_12 3850_1_ +9 - - 3850_5_10 - 3850_2_2 3850_1_2 + - - - 3850_6_3 - 3850_3_8 + - - - - - - + - - - - - - - + - - - 3850_3_10 3850_7_10 3850_3_6 + - - 3850_3_12 3850_8_4 - 3850_8_1 + - - - - 3850_1_10 3850_2_4 3850_2_ +12 - - 3850_7_1 - - - + - 3850_1_7 2 - - - - - - + - - - - - - + - - - - - - + - - 3850_1_12 - - - - + - - - - - - + - - 3850_6_10 - - - 3850 +_5_7 - - - - - 3850_1_ +9 - - 3850_5_10 - 3850_2_2 - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - 3850_3_12 - - - + - - - - - - + - - - - - - - + - - 2 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - 3850_1_ +9 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 2 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - 3850_1_ +9 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 3 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 3 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 3 +850_7_2 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 3850_3_3 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 3850_2_5 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 3850_7_10 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 3850_8_1 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 3 SPN23F SPCG SPJ spr SPP SPH + SPD HMPREF0837 SP SPG SP70585 SPT 385 +0_8_3 3850_8_2 3850_7_11 3850_8_7 3850_1_6 3850_2_10 3850_5_ +12 3850_8_8 3850_1_12 3850_1_11 3850_2_8 3850_1_1 3850_1_4 + 3850_7_4 3850_2_7 3850_6_7 3850_5_9 3850_6_11 3850_5_5 + - 3850_6_9 3850_6_10 3850_1_8 3850_3_1 3850_6_2 3850 +_5_7 3850_6_12 3850_3_9 3850_1_3 3850_6_8 3850_7_12 3850_1_ +9 3850_7_6 3850_6_5 3850_5_10 3850_6_6 3850_2_2 3850_1_2 + 3850_6_4 3850_5_3 3850_5_1 3850_6_3 3850_8_9 3850_3_8 38 +50_5_6 3850_7_8 - 3850_2_1 3850_3_5 3850_8_5 3850_3 +_11 - 3850_3_7 3850_2_3 3850_8_11 3850_7_5 3850_5_4 + 3850_6_1 3850_2_6 3850_5_8 3850_3_10 - 3850_3_6 3 +850_5_11 3850_3_2 3850_3_12 3850_8_4 3850_8_12 - 385 +0_2_9 3850_8_6 3850_7_9 3850_3_4 3850_1_10 3850_2_4 3850_2_ +12 3850_1_5 3850_2_11 3850_7_1 3850_8_10 3850_7_7 3850_5_2 + 3850_7_3 3850_1_7 3 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - 3850 +_5_7 - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - 3 - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - 3850 +_5_7 - - - - - +- - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - - - - - - + - - - - - - + - - - - - - + - - - - - - - + - -
## my result so far
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3

20100928 Janitored by Corion: Added readmore tag

Comment on regular expessions question: (replacing words)
Select or Download Code
Replies are listed 'Best First'.
Re: regular expessions question: (replacing words)
by jethro (Monsignor) on Sep 27, 2010 at 12:28 UTC

    Less example data would have been better, less scrolling to do ;-)

    Also please use 'use warnings' and 'use strict'

    One error I can see is in this line: @storage = $second_num; In short, the line does nothing sensible, even prevents the array from getting filled and should be removed completely.

    Another problem: my ($first, $second) = split(/^\s{1}/, $organized); Using '^' in a split would only make sense if you wanted to split a file into separate lines (you also would need the regex-modifier m). But since you want to split one line, '^' makes this line simply a no-op. Also "\s{1}" is identical to "\s". So you could just write "split(/\s/, ..."

    @final_storage seems superfluous, but in case you need it, copying the array would be as easy as @final_storage= @storage;

    Finally, all this stuff you are doing step by step could be done by a simple regex if it is guaranteed that all elements except the first one have a space before them:

    while (my $organized = <DATA2>) { $organized=~s/(\s)\w+/$1z/g; print $organized; }

    UPDATE: Thanks to jwkrahn for noticing that \s in the replacement isn't really working. Changed to use parentheses and $1

      $organized=~s/\s\w+/\sz/g;

      You are, for example, replacing the string " something" with the string "sz" instead of " z".

      Hi Jethro, Thanks a lot for the useful reply. I will follow your advice and also try the script. I also just came up with this script and it worked. Any comments on it? (i would really appreciate it!!)
      #!usr/bin/perl my $FILENAME4 = "organized.txt"; open(DATA2, $FILENAME4); #remove all previous re-organized files my $remove_reorganized = "re-organized.txt"; if (unlink($remove_reorganized) == 1) { print "Existing \"re-organized.txt\" file was +removed\n"; } #now make a file for the ouput my $outputfile = "re-organized.txt"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n" +; exit; } while (my $organized = <DATA2>) { #do some re-organizing #sort out the group numbers first $organized =~ s/(\w+)[^(^\d+)(\s)]/z/g; my $organized2 = $organized; $organized2 =~ s/z(\d+)/z/g; print POS $organized2; }
      Thanks, $new_guy
        $organized =~ s/(\w+)[^(^\d+)(\s)]/z/g;

        In a regular expression [ ... ] is a character set. What you told perl to look for is a word followed by ONE character that is not a '(', ')','^' or '+' and neither a number nor a space character.

        Since the first word in your lines seems to be a single digit number (at least in your sample data), it is just coincidence that it isn't replaced. Any word of length 1 will not be replaced. Also any word aka element with a number or any of the other characters above as last character would not be replaced.

        In short, if these lines work for you, it probably is just a coincidence

        Maybe you should use more variable test data to check for edge cases, for example try:

        5 suf 6 va7 7dra de) e+f ed ed 5z5 nu3 b +4 s 5 + 33 44 55 z5 zb zzz zb z5 4zz

        PS: Please reread my first post, I had to correct an error in my regex

        my $FILENAME4 = "organized.txt"; open(DATA2, $FILENAME4);

        You should always verify that the file opened correctly.

        my $FILENAME4 = "organized.txt"; open DATA2, '<', $FILENAME4 or die "Cannot open '$FILENAME4' $!";

        #remove all previous re-organized files my $remove_reorganized = "re-organized.txt"; if (unlink($remove_reorganized) == 1) { print "Existing \"re-organized.txt\" file was +removed\n"; } #now make a file for the ouput my $outputfile = "re-organized.txt"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n" +; exit; }

        If you open the file for output instead of append then you don't have to delete the file first as that is a side effect when you open for output.

        # make a file for the ouput my $outputfile = "re-organized.txt"; open POS, '>', $outputfile or die "Cannot open file '$outputfile' to w +rite to because: $!";

        $organized =~ s/(\w+)[^(^\d+)(\s)]/z/g;

        You are using a regular expression that says: match one or more word characters followed by a single character that is not the character '(' or '^' or any digit or '+' or ')' or any whitespace, which does not make sense.    It could be that you do not understand how character classes work?

        Any comments on it?
        Yeah, use an indentation style that makes sense. Indentation is there for *humans* only. It isn't some sort of magical lube that makes your program runs faster, and all that matters is to have some of it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://862193]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2015-09-01 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (38 votes), past polls