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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split a line with multiple delimiters ?

Status
Not open for further replies.

anandraman

Programmer
Mar 4, 2001
6
US
Hi,

@linearr contains virtual_maps=hash:/etc/postfix/virtual

I want to convert @linearr as follows :-

virtual_maps hash /etc/postfix/virtual

so that I can assign $virtFile = $linearr[2];

Basically remove the '=" and ":" and leave a blank space Instead. so that I may use the split() to have blank space as my delimiter.

Can someone help me out

Thanks,
Anand
 
the 'tr///' function searches through a string and replaces each character that is matched in the first field by the corresponding character in the second field, defaulting to last character if there are too many in the first field. meaning: you could do this:[tt]
$array[0] =~ tr/:=/ /;[/tt]


but that's not very perlish, cause then you'd have to split each individual one up again to put them in their own individual arrays. here's what i think you could do: split on either character, as in the following:
[tt]
foreach ($array)
{
my @tmp = split(/\=|\:/, $_);
....process it
}
[/tt]

that'll give you the data in an array format. you could then even make an array of array references, so that you could maintain the original array structure, but still gain the increased flexability of the individual arrays... "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Ok ..i did something like this and got what i wanted.
Anyways thanks a bunch for your fast help/inputs

Thanks,
-Anand

@linearr = virtual_maps=hash:/etc/postfix/virtual

@linearr = split;
if (/=/){
@linearr = split(":", $_);
}
$virtFile = $linearr[1];
$virtDB = $linearr[0];
@splitarr = split("=",$virtDB);
$virtDB = $splitarr[1];

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top