Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Three Variable Split

Status
Not open for further replies.

jonnymp316

IS-IT--Management
Jun 6, 2003
6
US
Ok so what I want to do is to parse 1 line into 3 variables at 3 different points so here is what it looks like now:

my ($name, $value) = split /:/;

and here is what I want it to look like or do I guess:

my ($name, $value, $phone) = split /:/,/;

like I said its basically parsing one line at the colon:)) and the other at the comma(,) anyway if anyone could help that would be awesome.

Thanks,
-Jonny
 
split only allows one pattern match so you could use a group in your regular expression. Something like the following:

my ($name, $value, $phone) = split /[:,]/;

The caveat is that this is not positional. In other words if there is a , before a : then it will split on the comma first.
 
...or you could try
Code:
  $line =~ /^([^:]*):([^,]*),(.*)$/
    && my ($name, $value, $phone) = ( $1, $2, $3 );
which says grab every non-colon character from the top of the string into $1 (and thence into $name). Ignore the first colon, then grab every non-comma character up to the next comma into $2 (and thence into $value). Ignore the comma and put everything else into $3 (and thence into $phone).

Yours,


fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top