> On Mar 25, 2015, at 10:07 AM, Anirban Adhikary <anirban.adhik...@gmail.com> 
> wrote:
> 
> Hi List
> I have a configuration file and I would like to split the main file into 
> multiple small files and push the small temp. files into an array. My config 
> file looks like this 
> 
> GRC01;8;8;1;1;323U6_SIU-8;2048;2048;20;0;OFF
> (30 lines snipped)
> GRC01;335;335;32;32;335U6_SIU-335;2048;2048;20;0;OFF
> 
> This config file has got 32 lines so I am planning to write a code which will 
> create four tmp. files out of the main config file. I have written the 
> following code.
> 
> #!/usr/bin/perl
> use Tie::File;
> use strict;
> use warnings;
> 
> my @config_file_contents;
> tie @config_file_contents, 'Tie::File', "GRC01_TG_SCGR_IPM_LIST" ###<-- This 
> is main config file
>   or die "Can't open filename";
> 
> my @tmp_config_array=@config_file_contents;
> untie @config_file_contents;
> my $last_element_index = $#tmp_config_array;
> my $counter = 1;
> my $start_index = 0;
> my $next_index = 8;
> 
> for (my $i=0; $i<= $last_element_index; $i++) {

You are iterating over all of the lines in the configuration file, which is 32. 
I think you only want to execute this loop four times, one for each subfile.

> print "inside for loop\n";
>         print "START=$start_index\n";
>         print "NEXT=$next_index\n";
>         my $file_to_write = "GRC01_TG_SCGR_IPM_LIST_$counter";
>         my @tmp_line_content = splice 
> (@tmp_config_array,"$start_index","$next_index");
>         open my $WFH,'>',$file_to_write;
>         foreach my $el (@tmp_line_content) {
>                 print "EL=[$el]\n";
>                 next if /^\s*$/;
>                 print $WFH "$el\n";
>         }
>         close($WFH);
>         $start_index = ($next_index+1);
>         $next_index  = ($start_index+8);
>         $i = $start_index;
>         $counter++;
> #       @tmp_line_content = undef;
> 
> }
> 
> while executing the code 4 tmp files are created but all of them are zero 
> byte. Though in the print for first time  "EL"  I am getting 8 lines and 
> second time "EL" prints 
> 15 lines but line number 9-15 are skipped.Second time EL should start 
> printing from line number 9 instead it starts printing from line number 18.
> But the counters like $start_index,$next_index are incrementing properly.
> Also I am getting warning message "splice() offset past end of array at 
> ./tie_file.pl line 22”

After the first four iterations of your outer loop, $start_index will be beyond 
the last element index in @tmp_config_array, and @tmp_line_content will be 
empty. That is why your subfiles have zero bytes. The subfiles may be correct 
after the first four iterations, but they are getting overwritten in the 
subsequent 28 iterations.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to