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

the file content
DD: 11/09/2009 AU: jas dimaano PP: Employee ID list PP: (489459) Jas = DS16 -> with SPi since 2005/04/04 AND with ECO ever + since PP: Sam = FT35 -> resigned last 09-03-2008 PP: Evan = AT89 -> transferred last 20070605 to Journals PP: there's more... === DD: july 11, 2009 AU: Jr s. Tolentino, -editor PG: page 9 PP: Earn points now! PP: Yes! You heard it right! (635436)
my code foe the extract numeric id is
#!usr/local/bin/perl $filename = "input.txt"; open (FILE,"<$filename"); @fileinput=<FILE>; close(FILE); foreach $line(@fileinput) { my $test=($line); #add written by and capital the first letter of each word if($test=~s/AU: \b(\w+)|AU: \b(w+)\s\b(w+)/AU: Written by: \u$1 \u$2 +/g) { print $test; } }
the output went to DD: 11/09/2009 AU: jas dimaano PP: Employee ID list PP: Jas = DS16 -> with SPi since 2005/04/04 AND with ECO ever since PP: Sam = FT35 -> resigned last 09-03-2008 PP: Evan = AT89 -> transferred last 20070605 to Journals PP: there's more... === DD: july 11, 2009 AU: Jr s. Tolentino, -editor PG: page 9 PP: Earn points now! PP: Yes! You heard it right! <code>

Where's the numeric id go? how can I place it with the tag 'ID: ' above the AU: tag?

Replies are listed 'Best First'.
Re: Where is now the extract numeric id in the parenthesis
by Nikhil Jain (Monk) on Mar 21, 2011 at 07:59 UTC
    I would like to give you some general remarks:-

    1. Use three argument open for opening a file like

    open(my $fh, '<', "input.txt") or die $!;

    2. if you reading a file line by line then no need to put lines into array and then apply foreach loop on it, you can use while loop for that like

    while(my $line = <$fh>){ #insert data }
Re: Where is now the extract numeric id in the parenthesis
by JavaFan (Canon) on Mar 21, 2011 at 12:02 UTC
    If I run the program on your data, I get:
    AU: Written by: Jas dimaano AU: Written by: Jr s. Tolentino, -editor
    which is to be expected, as you only print something after you have modified it.

    Perhaps the code you are showing us isn't the code you are actually using?

    Note BTW that of the code you were showing, the second clause of the pattern in the substitution will never been matched, as anything that could potential match will be matched by the first clause.