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?'


In reply to Problems reading records from file by sanjay nayak

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.