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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on How can i read the lines and store them in an array like following fashion?
  • Download Code

Replies are listed 'Best First'.
Re: How can i read the lines and store them in an array like following fashion?
by reneeb (Chaplain) on Oct 19, 2006 at 10:17 UTC
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array; { local $/ = "\nITEM"; open my $fh,'<','./item.txt' or die $!; while(my $entry = <$fh>){ chomp $entry; $entry = 'ITEM'.$entry unless $entry =~ /^ITEM/; push @array,[split /\n/,$entry]; } close $fh; } print Dumper(\@array);
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i read the lines and store them in an array like following fashion?
by Melly (Chaplain) on Oct 19, 2006 at 11:18 UTC

    Gah! I replied to the reaped node - c'mon sanjay, make an effort here... oh well, here's my reply to the reaped node...

    What have you tried so far? C'mon - show us the code... ;)

    Okay - here's some help...(untested)

    my @array; open(IFILE, "item.txt"); while(<IFILE>){ chomp; if(/ITEM NO\:/ && defined @array){ MySub(@array); undef @array; } push @array, $_; }
    Tom Melly, tom@tomandlu.co.uk
Re: How can i read the lines and store them in an array like following fashion?
by Velaki (Chaplain) on Oct 19, 2006 at 10:57 UTC

    You could always gather up the data into a list of array refs, each pointing at the block data elements. If the raw data file is small enough, you could always slurp the whole thing, and process it as below.

    #!/usr/bin/perl use strict; use warnings; undef $/; my @blocks = map { [ split /\n/ ] } ( split /(?=ITEM)/, ( map { s/#.*\n//; $_ } <DATA> )[0] ); print "$_\n" for @{ $blocks[1] }; # print block no. 2 __DATA__ #This is the ITEM file ITEM NO:1 [aaa] 111 [bbb] 222 [ccc] 333 ITEM NO:2 [ddd] 444 [eee] 555 [fff] 666 ITEM NO:3

    Yeah, it's kinda golfy.

    Hope this helped,
    -v.

    "Perl. There is no substitute."
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How can i read the lines and store them in an array like following fashion?
by johngg (Canon) on Oct 19, 2006 at 12:56 UTC
    If the file is not too big, slurp it into $_ and do a global regex match to get each "ITEM" and attendant lines which you can then split on newline. Like this

    use strict; use warnings; use Data::Dumper; { local $/; $_ = <DATA>; } my $raDataItems = [ map {[split m{\n}]} m{(ITEM.*?)(?=(?:\z|ITEM))}sg ]; my $dd = Data::Dumper->new([$raDataItems], [q{raDataItems}]); print $dd->Dumpxs(); __END__ ITEM NO:1 [aaa] 111 [bbb] 222 [ccc] 333 ITEM NO:2 [ddd] 444 [eee] 555 [fff] 666 ITEM NO:3 [ggg] 777 [hhh] 888 [iii] 999

    and the output is

    $raDataItems = [ [ 'ITEM NO:1', '[aaa]', '111', '[bbb]', '222', '[ccc]', '333' ], [ 'ITEM NO:2', '[ddd]', '444', '[eee]', '555', '[fff]', '666' ], [ 'ITEM NO:3', '[ggg]', '777', '[hhh]', '888', '[iii]', '999' ] ];

    I hope this is of use.

    Cheers,

    JohnGG

Re: How can i read the lines and store them in an array like following fashion?
by fenLisesi (Priest) on Oct 19, 2006 at 10:57 UTC
    Man, you just asked half of this question yesterday. The least you could do is mention that. You also seem to have posted this question twice -- it will probably get reaped soon. Please take a look at this node.
Re: How can i read the lines and store them in an array like following fashion?
by Melly (Chaplain) on Oct 19, 2006 at 11:16 UTC

    What have you tried so far? C'mon - show us the code... ;)

    Okay - here's some help...(untested)

    my @array; open(IFILE, "item.txt"); while(<IFILE>){ chomp; if(/ITEM NO\:/ && defined @array){ MySub(@array); undef @array; } push @array, $_; }
    Tom Melly, tom@tomandlu.co.uk