Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, can someone help me with this? Most of it i can do myself, its just removing a value at the end of element4 i cant do.

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
    How about this -
    my @elem = split /\t/, $line[4]; # split my ($id) = $elem[0] =~ /"(.*)"/; # capture $elem[0] = '"new_alias"'; # replace if ($elem[4] =~ /\salias:\s/) { # check $elem[4] =~ s/"$/, $id"/; # append id before last " } else { $elem[4] =~ s/_random text_\s/_random text_ alias: $id/; # prepend } $line[4] = join "\t", @elem; # rebuild
Re: Strip data from end of an element of an array
by Hena (Friar) on Nov 14, 2003 at 14:33 UTC
    Heres my take.
    # make array print properly $"="\t" # assuming this while loop goes through file while (<INFILE>) { # these should remove line changes chomp; s/\r$//o; # split and clean $line[0] my @line=split (/\t/,$_); $line[0]=~s/^"|"$//go; # get the aliases from line[4] # $1 should be aliases and word 'alias:' is removed $line[4]=~s/(?:alias:)? ([\d\s,]+)"$/$1/o; # place things back and add $line[0] $line[4].="alias: $1, $line[0]\""; # get the new alias and place it $new_alias my $new_alias; $line[0]="\"$new_alias\""; # assuming we want newline use this, change "\n" if want different print OUTFILE "@line\n" }

    This is untested so i cannot be sure it works.

    UPDATE:
    Forgot the "" in $line4, fixed that
      $line[4].="alias: $1, $line[0]\"";

      Both your and Roger's example snippets modify the 5th element. The original question said, "I need to manipulate the contents of the forth element to contain what i want." The 4th element's index is, of course, 3, not 4 (assuming nobody tinkered with  $[).


      Dave


      "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
        Hi Davido, I agree with you that I am modifying the 5th element. But from what I read in the original post, I think he actually meant the 5th element when he said the 4th element (index of 4, but the index starts from 0). I confirmed this by counting the number of elements in the data, and found that the 5th element was indeed what he refers to. :-)