in reply to Splitting and formatting a line

First off, let me say that Ovid's approach is the correct one. Split is much nicer than a regex in this situation.

That said, here's your code rewritten to do what you seem to have wanted:
#!/usr/bin/perl -w use strict; my ($ID, $DP, $LN, $FN, $NM); open(EMPLOYEES,"<employees") || die "Cannot Open"; while(<EMPLOYEES>) { if(/(.*):(.*):(.*):(.*):(.*)/) { #Supposed to parse $_ $ID = $1; $DP = $2; $LN = $3; $FN = $4; $NM = $5; print $ID; # print $DP; # print $LN; # So I can see if a value is there print $FN; # print $NM; # print $LN."\t".$FN."\t".$ID."\t".$NM; } } close(EMPLOYEES)

The (..) sequence you had only matches 2 characters, (.*) matches as many as possible within the constraints of the rest of the expression. The if statement now includes the print statements so they are only run if the values are accepted.

Vavoom

Replies are listed 'Best First'.
Re: Re: Parsing
by jynx (Priest) on Dec 19, 2001 at 13:19 UTC