Note that your code does not compile: you're missing a semicolon on the line my @file = @in (and I don't think that line is necessary in the first place, since @file never gets used), and you haven't declared $updateline. And a few comments on other parts of the code: your two open statements are incorrect in that the || operator is not the right thing to use here, see "open" Best Practices for why. Also, your comment "read all file names form dir except names started with dot" doesn't actually describe what the code is doing, so it's confusing, and you should check your opendir for errors as well. And I final nit: you don't need warnings and the -w on the shebang, drop the latter.

In regards to the actual issue: You're reading lines from the IN filehandle into the $file variable. Because of this, $_ does not contain the current line. Your outer for loop is putting each filename in $in, not $file. Also, note that lines read from the input file will still contain the newline character at the end, so you probably should chomp that so that appending something to the line works properly. Taking all that together, changing some variable names, and indenting your code properly:

#!/usr/bin/perl use warnings; use strict; opendir my $dh, 'Master' or die "opendir: $!"; my @files = grep { /\.txt$/ } readdir $dh; closedir $dh; for my $file (@files) { open my $ifh, '<', "Master/$file" or next; open my $ofh, '>', "Update/$file" or die "can't open file Update/$ +file: $!"; while ( my $line = <$ifh> ) { chomp($line); my $updateline = $line . $file; print $ofh $updateline, "\n"; } close $ofh; close $ifh; }

In reply to Re: Adding Filename to the end of each line by haukex
in thread Adding Filename to the end of each line by jalopez453

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.