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

dear monks, i had been trying to make a file analyzer which reads keys from a file named labels seperates them using the delimiters @ each line and then checking for the keys from other file en_1000 and hi_1000 and printing them on another. i wrote this code , but its outputting nothing. i tried to check it by placing a print @ the 3rd line of the main program. but still no clue.

#!/bin/env perl use strict; use warnings; use Data::Dumper; sub split_english() { open(ENGLISH,">>en_index") or die $!; my @engli=<ENGLISH>; foreach my $keys(@engli) { chomp($keys); my @key_en = split(/\:/,$keys); print ENGLISH @key_en; } my $key_enco= join '|',my @key_en; my $file2='/home/vikash/pro_1/en_1000'; open my $fh1, $file2 or die $!; my %key_en; while (<$fh1>) { while (m{\b($key_enco)\b}g) { $key_en{$1}++; } } open(INFOO,">>result"); print INFOO Dumper(\%key_en); close INFOO; } sub split_hindi() { open(HINDI,">>hi_index") or die $!; my @hin=<HINDI>; foreach my $key(@hin) { chomp $key; my @key_hin = split(/\;/,$key); print HINDI @key_hin; } my $key_hico= join '|',my @key_hin; my $file='/home/vikash/pro_1/main_database_hindi'; open my $fh, $file or die $!; my %key_hin; while (<$fh>) { while (m{\b($key_hico)\b}g) { $key_hin{$1}++; } } open(INFOOO,">>result"); print INFOOO Dumper(\%key_hin); close INFOOO; print "*************************************************************** +***********\n******************************************************** +******************"; } open(HANDLE, ">>/home/vikash/pro_1/labels") or die $!; my @keys=<HANDLE>; close HANDLE; print @keys; foreach my $line(@keys) { chomp($line); (my $english_index, my $hindi_index ,my $nua)=split(/\|/,$line); open (HANDLE,">en_index") or die $!; print HANDLE $english_index; open(HANDLE_HI,">hi_index") or die $!; print HANDLE_HI $hindi_index; split_english(); split_hindi(); }

Replies are listed 'Best First'.
Re: reading from and writing to a file
by toolic (Bishop) on Jul 14, 2011 at 13:17 UTC
    but its outputting nothing. i tried to check it by placing a print @ the 3rd line of the main program.
    To solve this mystery, change:
    open(HANDLE, ">>/home/vikash/pro_1/labels") or die $!;
    to:
    open(HANDLE, "</home/vikash/pro_1/labels") or die $!;
    Then you should see some output. See also:
      thanks for the help. really appreciate it much. the code somehow works with a littile more necessery modifications.
Re: reading from and writing to a file
by jethro (Monsignor) on Jul 14, 2011 at 13:18 UTC

    In various places in your code you open a filehandle with ">>", i.e. you open the file for appending, then in the next line you try to read from the file. Which won't work, as the filehandle is positioned at the end of the file (naturally, since you wanted to append).

    If you want to read from the file and then append, open it first for reading, read then close it and open it again for appending