chen li wrote on Wed, Sep 06, 2006 at 08:23:42PM PDT:
> --- Adriano Ferreira <[EMAIL PROTECTED]> wrote:
> > On 9/6/06, chen li <[EMAIL PROTECTED]> wrote:
> > > I need a regular expression to process some data
> > but
> > > get stuck. I wonder if anyone here might have a
> > clue.
> > >
> > >  input:
> > >  my $line='group A 1 2 3 4';# separated by space
> > >
> > >  results:
> > >  my @data=("group A ",1,2,3,4);
> > 
> > You barely need a regular expression for this. A
> > split followed by a
> > join of the first two items would do.
> > 
> >     @data = split ' ', $line;
> >     unshift @data, (shift @data . " " . shift @data
> > . " ");
> > 
> Hi Adriano,
> 
> The line code you provide doesn't work on my computer
> but based on what you say I change it into this line
> code and it works. 
> 
> unshift @data, join (' ',(shift @data, shift @data));
> 
> 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

- David

-- 
"It may be true that the law cannot make a man love me, but it can stop him
from lynching me, and I think that's pretty important."
    -- Martin Luther King Jr.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to