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


In reply to Strip data from end of an element of an array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.