Hi Anirban, On Wed, 25 Mar 2015 22:37:39 +0530 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 > GRC01;12;12;2;2;134S1_SIU-12;2048;2048;20;0;OFF > GRC01;25;25;3;3;016S1_SIU-25;2048;2048;20;0;OFF > GRC01;29;29;4;4;018S1_SIU-29;2048;2048;20;0;OFF > GRC01;31;31;5;5;060S1_SIU-31;2048;2048;20;0;OFF > GRC01;48;48;6;6;023S1_SIU-48;2048;2048;20;0;OFF > GRC01;49;49;7;7;023S1_SIU-49;2048;2048;20;0;OFF > GRC01;51;51;8;8;030S1_SIU-51;2048;2048;20;0;OFF > GRC01;52;52;9;9;031S1_SIU-52;2048;2048;20;0;OFF > GRC01;54;54;10;10;033S1_SIU-54;2048;2048;20;0;OFF > GRC01;69;69;11;11;078S1_SIU-69;2048;2048;20;0;OFF > GRC01;77;77;12;12;097S1_SIU-77;2048;2048;20;0;OFF > GRC01;86;86;13;13;070S1_SIU-86;2048;2048;20;0;OFF > GRC01;103;103;14;14;039S1_SIU-103;2048;2048;20;0;OFF > GRC01;110;110;15;15;047S1_SIU-110;2048;2048;20;0;OFF > GRC01;114;114;16;16;051S1_SIU-114;2048;2048;20;0;OFF > GRC01;115;115;17;17;052S1_SIU-115;2048;2048;20;0;OFF > GRC01;120;120;18;18;089S1_SIU-120;2048;2048;20;0;OFF > GRC01;125;125;19;19;140S2_SIU-125;2048;2048;20;0;OFF > GRC01;133;133;20;20;300S2_SIU-133;2048;2048;20;0;OFF > GRC01;140;140;21;21;315S2_SIU-140;2048;2048;20;0;OFF > GRC01;154;154;22;22;100S1_SIU-154;2048;2048;20;0;OFF > GRC01;156;156;23;23;103U6_SIU-156;2048;2048;20;0;OFF > GRC01;160;160;24;24;120S1_SIU-160;2048;2048;20;0;OFF > GRC01;162;162;25;25;126S1_SIU-162;2048;2048;20;0;OFF > GRC01;165;165;26;26;101S1_SIU-165;2048;2048;20;0;OFF > GRC01;175;175;27;27;302S1_SIU-175;2048;2048;20;0;OFF > GRC01;207;207;28;28;203S1_SIU-207;2048;2048;20;0;OFF > GRC01;225;225;29;29;321U6_SIU-225;2048;2048;20;0;OFF > GRC01;233;233;30;30;333S1_SIU-233;2048;2048;20;0;OFF > GRC01;238;238;31;31;338S1_SIU-238;2048;2048;20;0;OFF > 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. > Some comments on your code: > #!/usr/bin/perl > use Tie::File; > use strict; > use warnings; You may opt to use "use autodie;" here. > > 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; Doing all that with Tie::File is unnecessary. Just slurp the array (using File-Slurp-Tiny or Path-Tiny or whatever) or iterate it normally using «while (<>)». See: http://perl-begin.org/topics/files-and-directories/ (Note - perl-begin.org is a site I originated and maintain.) > 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++) { This is better done using a foreach loop: foreach my $i (0 .. $last_element_index) { } Or use keys(@array) if your Perl version ois recent enough. > 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"); What are you trying to do here? The intention of this splice is not clear enough. Also see: http://perl-begin.org/tutorials/bad-elements/#vars_in_quotes > open my $WFH,'>',$file_to_write; use autodie for an exception here. And note that '>' will overwrite/clobber the file instead of appending to it. the > 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; Why are you assigning to $i? It's the iteration variable - it seems like bad design. > $counter++; > # @tmp_line_content = undef; That's not the way to empty an array: Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ “So, who the hell is Qoheleth?” - http://shlom.in/qoheleth Selina: How may I be of service? Alan: Well, according to the Codex, we’re supposed to be slain by you. — http://www.shlomifish.org/humour/Selina-Mandrake/ Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/