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

use strict; use warnings; use DDD::test2; my $incr = 0; my @array=""; undef $/; open DATA, "san.txt" or die $!; my @blocks = map { [ split /\n/ ] } ( split /(?=ITEM NO:)/, ( map { s/\#.*\n//; $_ } <DATA> )[0] ); while(1) { @array = @{ $blocks[$incr] }; if (@array eq "") { print "There is no item remaining in the file"; last; } test2::REQUEST(@array); $incr++; } #test2.pm contains package test2; sub REQUEST (@) { my @array=@_; print "@array\n"; } 1; #Suppose san.txt file contains #This is the ITEM file #This is a perl config file # I am ok ITEM NO:1 [aaa] 111 ffff [bbb] 222 [ccc] 333 #This not taken ITEM NO:2 [ddd] 444 [eee] 555 0000 [fff] 666 #This is not taken ITEM NO:3 [ddd1] 4441 [eee1] 5551 [fff1] 6661 # I am sanjay ITEM NO:4 ... ... ...
When i run the above program, the output comes as
#This is a perl config file # I am ok
ITEM NO:1 [aaa] 111 ffff [bbb] 222 [ccc] 333 #This not taken ITEM NO:2 [ddd] 444 [eee] 555 0000 [fff] 666 #This is not taken ITEM NO:3 [ddd1] 4441 [eee1] 5551 [fff1] 6661 # I am sanjay ITEM NO:4 ... ... ... Can't use an undefined value as an ARRAY reference at test1.pl line 12 +, <DATA> chunk 1
Can you suggest me some suitable code so that while reading
the config file my program does not take the lines which is started with #. Also how to avoid the error
"Can't use an undefined value as an ARRAY reference at test1.pl line 1 +2, <DATA> chunk 1"
and also how can i operate the if block.
if (@array eq "") { print "There is no item remaining in the file"; last; }
Please Suggest me.

20061023 Janitored by Corion: Added formatting, put data in code tags

2006-10-23 Retitled by planetscape, as per Monastery guidelines

( keep:0 edit:9 reap:0 )

Original title: 'Suggest me some suitable code?'

Replies are listed 'Best First'.
Re: Problems reading records from file
by bingos (Vicar) on Oct 23, 2006 at 07:53 UTC

    The simplest way to parse config files would be use one of the many existing config file modules on CPAN, like Config::IniFiles for instance.

    Evaluating an array in a scalar context produces the number of items in the array. So your block should look like this:

    unless ( @array ) { print "There is no item remaining in the file"; last; }
Re: Problems reading records from file
by reneeb (Chaplain) on Oct 23, 2006 at 07:55 UTC
    You don't need a while-loop. You should use a for-loop.
    And the test if any element is in the array should be unless(@array){...}.
    You have to use the modifier g to delete "all" things from '#' to the end of line...

    #!/usr/bin/perl use strict; use warnings; { local $/; open DATA, '<',"san.txt" or die $!; my @blocks = map { [ split /\n/ ] } ( split /(?=ITEM NO:)/, ( map { s/#.*?\n//sg; $_ } <DATA> )[0] +); close DATA; for(@blocks){ my @array = @$_; unless(@array){ print "There is no item remaining in the file"; last; } print "@array\n"; } }
      Hi
      Thanks a lot for your suggestion? It works fine according to my requirment. But this block is not executed at the end .
      unless(@array){ print "There is no item remaining in the file"; last; }
      It does not print "There is no item remaining in the file" at the end.
Re: Problems reading records from file
by jwkrahn (Abbot) on Oct 23, 2006 at 09:19 UTC
    Can you suggest me some suitable code
    Sure:
    open FILE, '<', 'san.txt' or die "Cannot open 'san.txt' $!"; my $incr = -1; my @array; while ( <FILE> ) { chomp; next if /^\s*(?:#|$)/; $incr++ if /^ITEM NO:/; push @{ $array[ $incr ] }, $_; } test2::REQUEST( @array );