You only gave one sample input line and then showed some code that admittedly doesn't do what you want - that's of course why you are asking a question. I would suggest that you show a number of test cases demonstrating: a) what you want and b)what your code actually does. One example is usually just not enough to describe the entire behavior that you want.

Often combining split() and regex is very powerful. And can result in a regex that is easier to write and understand. The code below essentially says: delete the parens (if any) around the 2nd thing in the line where "things" are sequences of characters separated by spaces. This may or may not be all that you need, but without more than one test case, I have no way to determine that.

Also there can be reasons why it is desirable to "slurp" all of the data in a file into a memory resident array as in @fileinput=<$file>;, but in general when processing files line by line, this is not a good idea as it wastes memory and is slower than: read line, process line, read next line, etc. Also when doing a file operation, like "open" check the status of the open - it is very possible that an "open()" can fail: file does not exits, wrong permissions, etc.

As Grandfather points out, DATA is an already open file handle that can read data from within your Perl code as shown below. This is extremely useful for testing.

#!/usr/bin/perl -w use strict; while (<DATA>) { my ($id, $num, $rest) = split(/\s+/, $_, 3); $num =~ tr/()//d; #deletes parens in num field print "$id $num $rest"; } #prints: #PP: 899040 Jane Smith #PP: 899040 Jane (Smith) #PP: 899040 Jane (Smith) __DATA__ PP: (899040) Jane Smith PP: 899040 Jane (Smith) PP: (899040) Jane (Smith)

In reply to Re: Extracting the number in a file and get the value and output to the other file by Marshall
in thread Extracting the number in a file and get the value and output to the other file by allison

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.