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!

using the split function but not splitting | char

Status
Not open for further replies.

yahoo182

Programmer
Jul 5, 2005
70
CA
Hi there, I have a script that splits the fileds seperated by a special character. The code works fine when the character is , but does not when I have a textfile seperated by |

My code is as follows. Can anyone give me some hints ?

Thanks :)


open(FILE, "test.txt") or die "$!";
my $string = <FILE>;
close(FILE);
chomp $string;
my @list = split(/|/,$string);

open(FILE, ">4newfile.txt") or die "$!";
print FILE "$_" for @list;
close(FILE);
 
escape the | (pipe) in the regex

i.e.

\|


Kind Regards
Duncan
 
Hi

If the separator is a fixed character, then is simple to just escape it with a backslash. But when is stored in a variable, so you want something like [tt]split /$sep/,$string;[/tt], then escaping becomes a bit interesting. In this case could be easyer to quote the metacharacters in the expression.

Code:
split /[red]\Q[/red]$sep[red]\E[/red]/,$string;

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top