Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I basically have a file and each line is read into an array called line. I need to manipulate the contents of the forth element to contain what i want.
I have a script that goes through another file to get some reference number i need and appends it to the end of the forth element, hence the comma sepearated num values.
The 4th element either contains some sort of text (some notes on computer componenets), the word 'alias:' and some reference numbers with one appended to the end, in this case ', 7654321':
"246810"<tab>"e1"<tab>"e2"<tab>"e3"<tab>"_random text_ alias: 1010101, + 1234, 7654321"<tab>"e5"
or it contains some notes and will have the appended reference number on the end.
"246810"<tab>"e1"<tab>"e2"<tab>"e3"<tab>"_random text_ , 7654321"<tab> +"e5"
What i want to happen is if 'alias:' is in the forth element, just append element0 to the end, in this case 246810, and replace element0 with a new alias to give:
"new_alias"<tab>"e1"<tab>"e2"<tab>"e3"<tab>"_random text_ alias: 10101 +01, 1234, 7654321, 246810"<tab>"e5"
Else i want alias inserted between '_random text_' & the comma and element0 appended to the end as before. So i thought the appended value (, 7654321) could be striped and stored as $last, append 'alias:' to the end of $line[4].$alias.$last.$last[0]
Heres my attempt below, I think ive figured out more or less everything out other than the 'stripping' of the appended value
my $alias = " alias:"; my $palias = countP(); # refers to a sub process which gets a new alia +s number if ($line[4] =~ /\b$alias\b/) { # And strip the quotes from the notes for later. $line[4] =~ s["([^\x22]*)"][$1]; # And strip the quotes from the element0 for later. $line[0] =~ s["([^\x22]*)"][$1]; # Add element0 to end of element4 my $elzero = $line[0]; $line[4] = "\"$line[4].$elzero\""; # glue the elements together with tabs $lines = join "\t", @line; # replace the A alias with the a P alias & attach quotes $lines =~ s/$line[0]/\"$palias\"/; # increase the $palias by 1 $palias++; # remove carriage returns $lines =~ tr/\012\015//d; # print result to final output file print OUTFILE $lines; } else { # And strip the quotes from the notes for later. $line[4] =~ s["([^\x22]*)"][$1]; # And strip the quotes from the element0 for later. $line[0] =~ s["([^\x22]*)"][$1]; ## remove last value and store as $last ## # remove leading comma from $last $last =~ s/,//g; # Add element0 to end of element4 my $elzero = $line[0]; $line[4] = "\"$line[4].$alias.$last.$elzero\""; # the contents of the notes field is non empty # glue the elements together with tabs $lines = join "\t", @line; # replace the A alias with the a P alias & attach quotes $lines =~ s/$line[0]/\"$palias\"/; # increase the $palias by 1 $palias++; # remove carriage returns $lines =~ tr/\012\015//d; # print result to final output file print OUTFILE $lines; }
Thanks for your help, Steve
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strip data from end of an element of an array
by Roger (Parson) on Nov 14, 2003 at 11:54 UTC | |
|
Re: Strip data from end of an element of an array
by Hena (Friar) on Nov 14, 2003 at 14:33 UTC | |
by davido (Cardinal) on Nov 14, 2003 at 16:38 UTC | |
by Roger (Parson) on Nov 14, 2003 at 23:56 UTC |