Hello mmazlan67,

Well fellow monk hippo provided you with the answer to your problem already, but I would like to point out some things to your code just for your knowledge.

Let's start. When you use die always use $! why? (from documentation)

If an uncaught exception results in interpreter exit, the exit code is + determined from the values of $! and $?

Moving on, when reading the file while (<$readhandle>) I would suggest always chomp $_; each line. The most important affect of chomp is that removes the new line characters \n it can produce problems or not desired output. Next step is that when you are using the general variable $_ you do not need to define it, Perl does this for you. For example, you do not need to define it like this chomp $_; you can do it straight like chomp;, same thing applies also on the if ($_=~ m/^Name=(\S+)/) you can do it straight like if (m/^Name=(\S+)/).

Moving on, I noticed on your first if condition you define the regex without a space after the equal sign if ($_=~ m/^Name=(\S+)/) but on your input data file you have a space, so which one is the correct?

Last thing that I noticed is that when you are writing on your file your are overwiriting it, if you want to appent it you need to use >>.

I would also recommend to use the say. It automatically appends at the end of the string a new line character \n so you do not to be bothered to enter it when you are debugging or writing to a file.

Any way based on my recommendations and correct solution provided by the fellow monk hippo here is a working version of your code.

#!/usr/bin/perl use strict; use warnings; use feature 'say'; say "Enter the name of the file to read: "; my $filetoread = <STDIN>; chomp($filetoread); say "Enter the name of the file to write: "; my $filetowrite = <STDIN>; chomp($filetowrite); open my $readhandle, "<", $filetoread or die "Unable to read '$filetoread': $!"; open my $writehandle, ">>", $filetowrite or die "Unable to write '$filetowrite':$!"; while (<$readhandle>){ chomp; if (m/^Name=(\S+)/) { say $writehandle $1; } if (m/^Path=(\S+)/) { say $writehandle $1; } } close $readhandle or warn "Unable to close ".$filetoread.": $!"; close $writehandle or warn "Unable to close ".$filetowrite.": $!"; __END__ $ cat out.txt blocka /main/sub/a/bg/adee/swarfrw/ewqsx/fewaxr/akeil/awedr/date/file

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4. by thanos1983
in thread error: Use of uninitialized value in print at enhancement.pl line 31, <$readhandle> line 4. by mmazlan67

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.