--- David Romano <[EMAIL PROTECTED]> wrote:
> chen li wrote on Thu, Sep 07, 2006 at 01:13:04PM > PDT: > > > > > One more question what if I have a file that > have > > > > different lines 1) some lines have number only > 2) > > > some > > > > lines have more than 2 words at the begining? > > > > > > > > my $line1='1 1 1 1 1'; > > > > my $line2='group A 2 2 2 2"; > > > > my $line3= 'group B and C 3 3 3 3"; > > > > > > > > Do you think I need a if statement to do the > job? > > > > > > If you want to use a regex for all these, the > > > following might work with > > > your data: > > > use strict; > > > use warnings; > > > > > > $"=','; > > > for (<DATA>) { > > > my @data = m/(\D+[^\d\s]|\d+)/g; > > > print "@data\n"; > > > } > > > > > > __DATA__ > > > 1 1 1 1 1 > > > group A 2 2 2 2 > > > group B and C 3 3 3 3 > > > > Thank you for the lines. But what are the meaning > for > > 1) $"=','; > This is the separator between array elements that > are double-quote > interpolated. See perldoc perlvar. Basically, if you > interpolate an > array (e.g., @data) in a string (e.g., "@data"), the > default separator > is a single space. You can change that to any > character you want. > Usually it's best to say: > > local $"='whatever'; > > so that any interpolation outside the current block > won't be affected. > > > 2) @data = m/(\D+[^\d\s]|\d+)/g; > This is a regex which automatically uses whatever is > in $_, and stores > its matches into the @data array. The for loop > automatically assigns > $line1, $line2, and $line3 to $_ for each iteration > of the for loop, and > so m/.../g matches against the value in $_. The > values (from your data) > can either be more than one non-digit followed by a > character that isn't > a space and isn't a digit (the \D+[^\d\s] part) or > more than one digit > (the \d+) part. It matches greedily (the /g part ), > and each match is > returned when called in list context. For this case, > it's stored in > @data. Hope that helps, and try to keep the > responses to the list, > since then others can see the on-going discussion as > well. > > - David Thank you very much David! One more question about this regex: @data = m/(\D+[^\d\s]|\d+)/g; I check Programming Perl or perldoc they say ^ is used as an anchor meaning "start/begining with". But here looks like it has a different usage. Is that right? Li __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>