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


Dear Wise Monks,

I would like , when I open a file, to select some elements

Here is my start file
AAD; ;CCR;365 TEC;UTF;TDE;450 TRE; ;FTE;164 TEC;URT;TDE;687 ERT; ;ETR;567 TEC;UGC;TDE;687

I would like to work only on the lines for which the second element is not empty, that means the following lines :
TEC;UTF;TDE;450 TEC;URT;TDE;687 TEC;UGC;TDE;687

Could you tell me where did I go wrong? Thanks
#!/usr/bin/perl use strict; use warnings; use diagnostics; # la commade "use strict" permet d'être plus rigoureux ############################################## # TASKS MADE BY THE SCRIPT ############################################## # the task of this script is to look for the following aircraft_id ################################## ## ARGUMENTS OF THE SCRIPT ################################## ##################################### # do not forget the $ before the ARGV ##################################### # THE FIRST ARGUMENT : # we have to give the name of the regulation which will help us to ope +n the first file # the name of the first file to open is : Regulation_Slot_List_${reg_ +id} my ${Reg_Id} = "$ARGV[0]"; ################# END OF THE DECLARATION OF THE ARGUMENTS ################# IN WHICH DIRECTORY WE ARE ######################## my $Current_Dir = `pwd`; print STDOUT "the current directory is $Current_Dir"; ##################################################################### ################### OPEN THE ANALYSIS INFILE ######################### +### # open the first file # do not forget the "" to declare my ${analysis_file} = my ${analysis_file} = "Analysis_Regulation_Slot_List_${Reg_Id}_last.cs +v"; open(INFILE,"${analysis_file}") or die "Can't open ${analysis_file} : +$!"; ###################################################################### +##### ################### OPEN THE OUTFILE ############################ # open the first file # do not forget the "" to declare my ${analysis_file} = my $outfile = "with_ALL_FT_${analysis_file}"; open(OUTFILE,">${outfile}") or die "Can't open ${outfile} : $!"; ###################################################################### +##### # we want to skip the first line of the INFILE <INFILE>; while (<INFILE>){ my @Elements = split(/;/,$_); if ($Elements[1] ne ' '){ print OUTFILE "$_"; } } close INFILE; close OUTFILE;
update : proposition of code

Replies are listed 'Best First'.
Re: select elements in a list
by FunkyMonk (Bishop) on Aug 06, 2007 at 14:24 UTC
    split each line on ; and test if the second element in the list is 3 spaces:
    my @interesting = grep { (split /;/)[1] ne ' ' } <DATA>; print @interesting; __DATA__ AAD; ;CCR;365 TEC;UTF;TDE;450 TRE; ;FTE;164 TEC;URT;TDE;687 ERT; ;ETR;567 TEC;UGC;TDE;687

    Output:

    TEC;UTF;TDE;450 TEC;URT;TDE;687 TEC;UGC;TDE;687

    Not nearly as much fun as your previous question!

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: select elements in a list
by Fletch (Bishop) on Aug 06, 2007 at 14:23 UTC

    Use grep on the results of splitting your source lines on ;. If you just want to filter the file judicious use of the -F, -a, and -n options (see perlrun) for a one-liner would do the trick.

Re: select elements in a list
by toolic (Bishop) on Aug 06, 2007 at 14:27 UTC
    This code demonstrates some basics about opening and closing a file, using a regular expression to search for specific lines, and using strictures (warnings/strict) to enforce good coding practices.
    #!/usr/bin/env perl use warnings; use strict; open my $fh, '<', 'foo.txt' or die "Can not open file: foo.txt: $!\n"; while (<$fh>) { if (/^\w{3};\w/) { print; # or do whatever you need to do... } } close $fh or die "Can not close file: foo.txt: $!\n";
    Here is the output, assuming "foo.txt" is your start file:
    TEC;UTF;TDE;450 TEC;URT;TDE;687 TEC;UGC;TDE;687
Re: select elements in a list
by hilitai (Monk) on Aug 06, 2007 at 14:26 UTC
    Well, here's a 10-second version:
    #!/usr/bin/perl use strict; use warnings; use diagnostics; my $file = shift; open (F,"<$file") or die "Can't open $file: $!"; while (<F>) { $_ !~ /;\s+;/ and print $_; }
Re: select elements in a list
by merlyn (Sage) on Aug 07, 2007 at 17:25 UTC
    Just zeroing in on something nobody else has yet commented on, you have:
    my $Current_Dir = `pwd`; print STDOUT "the current directory is $Current_Dir";
    First, if you're using pwd like that, you should chomp it. The directory name doesn't end in a newline (normally), but the output of pwd has an extra newline on the end. This would have broken things later had you done something like:
    open my $output, ">", "$Current_Dir/foo.txt";
    as the extra newline would have caused the directory to not be found.

    Second, you could have used Cwd, which is a core module, and done this much faster and more reliably:

    use Cwd; my $Current_Dir = getcwd; print STDOUT "the current directory is $Current_Dir\n";