For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Pattern matching to identify comma separated text









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Pattern matching to identify comma separated text
Chris Schults

2005-03-09, 3:56 am

I'm sending this on behalf of our intern Elmer. Thanks in advance for any
assistance. Chris

Hi there!

If anyone out there is good with Perl's pattern matching, maybe you can help
me out. I am trying to take a string and derive information from it that is
separated by commas so I can then analyze the individual values.

So if I have a string like: 'Associated Press, John. D. Reporter, 01 Apr
2005'

I want to be able to scan it, and extract 'Associated Press' 'John. D.
Reporter' and '01 Apr 2005' as distinct values--maybe store them as
variables so I can manipulate them.

It should also be that the number of data items not be relevant. In case
the string is like 'Joe Freelancer, 01 May 2005' or 'Reuters, 01 Jun 2005'.
Or if it has no commas and is just '01 Jul 2005'.

Kind regards,

Elmer Zahraie


Wiggins d'Anconia

2005-03-09, 3:56 am

Chris Schults wrote:
> I'm sending this on behalf of our intern Elmer. Thanks in advance for any
> assistance. Chris
>
> Hi there!
>
> If anyone out there is good with Perl's pattern matching, maybe you can help
> me out. I am trying to take a string and derive information from it that is
> separated by commas so I can then analyze the individual values.
>
> So if I have a string like: 'Associated Press, John. D. Reporter, 01 Apr
> 2005'
>
> I want to be able to scan it, and extract 'Associated Press' 'John. D.
> Reporter' and '01 Apr 2005' as distinct values--maybe store them as
> variables so I can manipulate them.
>
> It should also be that the number of data items not be relevant. In case
> the string is like 'Joe Freelancer, 01 May 2005' or 'Reuters, 01 Jun 2005'.
> Or if it has no commas and is just '01 Jul 2005'.
>
> Kind regards,
>
> Elmer Zahraie
>
>
>


Check out the 'split' function.

perldoc -f split

my @items = split /,/, $string;

Is the simplest, but you may need to take into account whitespace, etc.
Also note that if you are parsing data that is coming in a true CSV
format, aka with quoted values that might contain commas, etc. then you
should consider using a module designed for this purpose, such as
something in the Text::CSV range.

http://danconia.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com