in reply to Re: Selection of a part of a text
in thread Selection of a part of a text


Thanks a lot Miller for your reply. However, it does not seem to work

Here is my start file :
141 142 143 REGULATION SLOT LAYOUT + REGULATED FLIGHTS 144

I have tried to replace the text by the number (here 143) and it works

But it seems that the number at which step the text "REGULATION SLOT LAYOUT + REGULATED FLIGHTS" is likely to change.

Where did I go wrong ?
Here is my code
my $file = "$ARGV[0]"; ################# END OF THE DECLARATION OF THE ARGUMENTS my $Current_Dir = `pwd`; # print STDOUT "the current directory is $Current_Dir"; # open the ALL_FT file open(INFILE,"$ARGV[0]") or die "Can't open $ARGV[0]: $!"; # name of the OUTFILE # do not forget the "" if # never put a \n at the end of the OUTFILE name otherwise it does not +create the output my ${outfile_name} = "bon_format_$file"; # to open the file # OUTFILE is the name of the HANDLE in this case open (OUTFILE, ">${outfile_name}.csv") or die "Can't open ${outfile_ +name}.csv: $!"; my @Parts; my $part; while (<INFILE>) { if (/^143/..eof(INFILE)){ # the lines are composed of elements separated by a point comma my $Line = $_; my @Elements = split(";", $Line); my $element = $Elements[1]; @Parts = split(" ",$element); foreach $part(@Parts){ print OUTFILE ";$part"; } print OUTFILE "\n"; } } close INFILE; close OUTFILE;

Replies are listed 'Best First'.
Re^3: Selection of a part of a text
by GrandFather (Saint) on Jul 30, 2007 at 10:28 UTC

    You may find:

    use strict; use warnings; 1 while defined ($_ = <DATA>) and ! m/^REGULATION SLOT/; while (<DATA>) { my @Elements = split ";"; my $element = $Elements[1]; my @Parts = split " ", $element; print join (';', @Parts), "\n"; } __DATA__ Header line to be skipped REGULATION SLOT LAYOUT + REGULATED FLIGHTS element 1; element 2

    a somewhat cleaner technique. Note that the I/O using external files has been replaced by DATA and STDOUT for purposes of demonstration.


    DWIM is Perl's answer to Gödel
Re^3: Selection of a part of a text
by steph_bow (Pilgrim) on Jul 30, 2007 at 10:11 UTC

    Excuse me


    the error was due to the ^ in the /^ ..../ that makes the programm only accept the line that begins with the text indicated.

    So I made the ^ dissapear