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

Hi there,
I am trying to process a small file to output some contents out of it. My input file is as below:
model_techlib \ -tech_lib_file libfile.teclib \ -device_name 12x3X1V1 -variation 0.25 output_setup \ -device_file_name 12x3X1V1DEVFILEDONOTTOUCH \ -map_file_directory_name ./ processing_setup \ -statistics_conversion_map \ A1 B1 \ CC1 DD1 \ A2 B2 \ A3 B3 \ A4 Bl4 \ A5 B5 \ A6 B6\ A56 Z56 variation_input_db \ -type signsample \ -PHI_file ../12x3X1V1.PHI \ -PHI_LIBRARY_LIST PHI_LIB.list sampling -type ORTHOSAMPL results_database \ -type PHI \ -print_sigma true print_gmm -remove_non_convergence true \ -merge_asymptoses true print_gmm_sigma -total_sigma_limit 1.0 solve \ -samples all \ -type PHI #solve \ #-samples all \ #-type PHI

My Perl program to process the above file is as shown below:
#!/usr/bin/perl use strict; use warnings; open(IFH,"<","testlib.tcl") || die "Cannot open input file","\n"; my $line =""; my $cmd =""; my @arrC = (); while(<IFH>) { next if(/^#/g); next if(/^\n/g); s/^\s+//g; if(/\\\s*$/) { s/\\\s*//g; chomp; $line .= $_; } else { $line .= $_; } } #print $line; my @arr = split(/\n/,$line); my @test = (); foreach (@arr) { if(/variation_input_db/) { s/variation_input_db\s*//g; @test = split(/-/,$_); } } foreach (@test) { print $_,"\n"; }

The above file is saved as "test.pl". I executed the program as below:
neal@alnz1:~/JUNK/JUNK1$ ./test.pl

The output from the file always shows the first line as a blank line followed by the other elements of the array as below:
type signsample PHI_file ../12x3X1V1.PHI PHI_LIBRARY_LIST PHI_LIB.list neal@alnz1:~/JUNK/JUNK1$

can some one please help why I see a blank element in my array @test ?
Thanks to you all.
Neal

Replies are listed 'Best First'.
Re: blank line in array printing
by ikegami (Patriarch) on Feb 23, 2009 at 03:11 UTC
    I'm guessing it's related to the incorrect use of the "g" modifier in scalar context. Even if it isn't, the following is still buggy:
    next if(/^#/g); next if(/^\n/g);

    Also, you check for empty lines, but not for lines that contains nothing but spaces.

    How about:

    while(<IFH>) { s/#.*//; if (s/\\\s*$//) { $_ .= <IFH>; redo; } s/^\s+//; s/\s+$//; next if !length(); $line .= $_; }
Re: blank line in array printing
by kennethk (Abbot) on Feb 23, 2009 at 03:13 UTC

    Your output begins with a blank line because the string you are splitting is -type signsample -PHI_file ../12x3X1V1.PHI   -PHI_LIBRARY_LIST PHI_LIB.list. The string starts with a hyphen, so the first split element is a null string.

    As a second note, you should get in the habit of using or in place of || for the phrase or die if you don't want to get Burned by precedence rules.

    Update: Prior to that split, the matching @arr element is variation_input_db -type signsample -PHI_file ../12x3X1V1.PHI   -PHI_LIBRARY_LIST PHI_LIB.list, so perhaps you meant s/variation_input_db\s*\-//g;?