in reply to Re: Applying multiple file modes
in thread Applying multiple file modes

Thanks for the response, but i have tried with the given solution but getting error during while condition. Plse check the below ++++++++++++++++++++++++++++++++++++
use strict; use warnings; my $file = 'aaa.txt'; if ( !-e $file || -z $file ) { open( my f1,'>', $file ) or die "unable to open output log file: $resu +lts. Error: $!\n"; print James|chicago "\n"; close( $f1 ); #close output file handle } else { open( my $f2, '+>>', $file ) or die "unable to open output log file: $ +results. Error: $!\n"; while (<$f2>) { print "test2"; chomp $_; @fields = (); @fields = split ('\|', $_); $flag = $fields[0]; if ( $flag eq inputdata ) -> comes from input file { print already $inputdata exists"\n"; $exitCode = 0; } else { # prints the next line of the sync edit results Add the data to the existing file $exitCode = 0; } } seek $data, 0, SEEK_SET; close( $f2 ); #close output file handle }
o/p file
james|chicago - first run alradeyd james exists - second run since input valeue is james and its + akready exit charles|chicago - third time run charles is not exists so written to t +he file

Replies are listed 'Best First'.
Re^3: Applying multiple file modes
by Athanasius (Cardinal) on May 26, 2015 at 03:29 UTC

    Hello again leelu,

    The monks are happy to help you, but you need to meet us half-way. First, please follow wrog’s advice and format your post using paragraph and code tags:

    <p>First paragraph</p>
    <code>
    use strict; use warnings; ... close( $f2 ); #close output file handle }
    </code>
    <p>Final paragraph</p>

    See:

    Second, please follow afoken’s advice and post actual code. The code you have posted does not compile:

    • The $ sigil is missing from the first occurrence of $f1.
    • You have use strict but no declarations for the variables $results, @fields, $flag, and $inputdata.
    • The line print James|chicago "\n"; contains unquoted text; likewise the line print already $inputdata exists"\n";.

    Third, you say you are “getting error during while condition,” but don’t explain what that error is. Please see:

    and be sure to show the output you want to see, and exactly how it differs from the output you do see.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,