in reply to Strip data from end of an element of an array

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

Replies are listed 'Best First'.
Re: Re: Strip data from end of an element of an array
by davido (Cardinal) on Nov 14, 2003 at 16:38 UTC
    $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. :-)