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

I am trying to open a file and extract certain contents of the file and place it in a different file. My code is not throwing error but its running without producing an o/p here comes my code
#!/usr/local/bin/perl -w my $filein = ""; $filein = $ARGV[0]; #print "$filein"; #$fileout = ""; #$fileout = $ARGV[1]; $argcnt = $#ARGV + 1; if ($argcnt != 1) { print "usage: perl extract.pl input_file\n"; } else { print "I am here !!!"; print "$filein"; open(MYINPUTFILE, "$filein") || die ("could not open file"); #open(MYOUTPUTFILE, ">$fileout"); @data = <MYINPUTFILE>; close(MYINPUTFILE); #while(<MYINPUTFILE>) foreach $line (@data) { # my($line) = $_; chomp($line); print "check1"; if ( $line =~ m/^.*\|.*(\|.*\|).*(\|.*\|.*\|.*\|).*(\|.*\|).*\|.*\|.* +\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\|.*\ +$/ ) { #my($zip,$name,$mm,$dd,$yy) = ($1,$2,$3,$4,$5); print "check2"; $var1 = $1; $var2 = $2; $var3 = $3; #my($first, $last) = split(/ /, $name); # $line = sprintf("%-16s%-10s%02d/%02d/%04d%5d", # $last,$first,$mm,$dd,$yy,$zip); print $var1.$var2.$var3 #print MYOUTPUTFILE "$line\n"; } } } print "check4"; #close(MYINPUTFILE); #close(MYOUTPUTFILE);
The sample input file which has lines like as follows
OR|xxxxx61|xxxxxxx|xxxxxxx|-|xxxxx|-|xx|xxxxx|xx|-|xxxx|BEAVERTON|OR|x +xxx|TN[xxxxxxx]|x|x|HASxxxxxxxx|x|xxxxxxxxx|xxxxxxxx|xxxxxxx|1|Vxxxx| +4xxxxxxx|-|ALOHORxxxxxxxxxx

Replies are listed 'Best First'.
Re: Parsing delimited file contents
by gmargo (Hermit) on Nov 02, 2009 at 18:14 UTC

    First of all, please use strict and use warnings and for good measure use diagnostics. Those three lines will save you a lot of pain.

    Second of all, your regular expression is "not working", because you have escaped the dollar sign at the end of your pattern. So the pattern is looking for a literal dollar sign, not the end of line as I presume you intended.

    Third of all, that is one remarkably inefficient regular expression! It took over a minute to run on my machine. If you just change each greedy ".*" to a non-greedy ".*?" it runs in no time. (Also your one line of sample data is missing a trailing "|" symbol.)

Re: Parsing delimited file contents
by CountZero (Bishop) on Nov 02, 2009 at 20:33 UTC
    Whenever you have to deal with delimited data, think of using Text::CSV. It deals with a lot more than just "comma" separated values.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Parsing delimited file contents
by zentara (Cardinal) on Nov 02, 2009 at 16:51 UTC
Re: Parsing delimited file contents
by toolic (Bishop) on Nov 02, 2009 at 17:03 UTC
    Try to split your input:
    my @tokens = split /\|/, $line; print "@tokens[0..2]\n";
Re: Parsing delimited file contents
by mikelieman (Friar) on Nov 02, 2009 at 17:12 UTC
    All the code which opens and sends output to the file appear to be commented out.

    I'd fall back to a trivial example. Open file, loop through, print read values. ( which it appears you're sorta-trying to do here ) Then when you have it showing you that it's doing the open/read/print/loop thing right, you can open the second file, and write the output there. Once THAT'S working, then just transform your data and you're done.