CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How do I... User definable split character

 



S_Shrum
User

Jan 9, 2001, 2:40 PM

Post #1 of 10 (801 views)
How do I... User definable split character Can't Post

I have written a flat file to html table script that allows for a number of various variables.

I personnaly use the pipe character to delimit my fields as I use commas in my data. I would like to set up a variable to let the user specify what the fields are seperated with so I can pass this to the split function to better increase the script's functionality and useability.

I tried (and would like it to) pass(ing) the entry directly like:

split /$input{'seperator'}/,...

...but that doesn't work. What do I need to do to make the variable work with the split function directly? I don't want to have to hard code some sort of case statement and evaluate the entry like...

#pseudocode
case ',' { split /,/...};
case '|' { split /|/,...};
...

Thanx in advance.

Sean Shrum
sean@shrum.net



sleuth
Enthusiast / Moderator

Jan 10, 2001, 12:34 AM

Post #2 of 10 (793 views)
Re: How do I... User definable split character [In reply to] Can't Post

 
I don't see why

$dilimiter = "$input{'seperator'}";
...
(blabla)=split(/$dilimiter/, $_);

Wouldn't work.

Sleuth



zanardi
journeyman

Jan 10, 2001, 6:28 AM

Post #3 of 10 (791 views)
Re: How do I... User definable split character [In reply to] Can't Post

$dilimiter = $input{'seperator'};

no need for quotes :D

My BBS


S_Shrum
User

Jan 12, 2001, 11:16 PM

Post #4 of 10 (777 views)
Re: How do I... User definable split character [In reply to] Can't Post

Well, normally I guess you would be right however...

In the case of special characters (i.e. the pipe character I refered to in my initial post); the syntax is like:

$field = split /\.../, $_

...it is the "\" (literal?) that is causing problems.

Example: create a flatfile out of the following data:

field1|field2
Sean|Shrum
The|Sleuth

Now, write the script to take the file and split the fields using a variable like:
$variable = "|";
$data = split /$variable/, ...

or

$data = split /\$variable/, ...

...bearing in mind that some characters need to have the pre-\ (like pipe characters) and some don't (like commas). I tried both; either does not work or it splits on every character.

I hope this makes sense to someone out there.

Sean Shrum
sean@shrum.net

PS. I still need to clean it up but if you wnat to give the script a look, I have moved a copy of the script to my site at:

http://www.shrum.net/db_table.pl

It currently is hard coded to handle the split function with "|"

PSS. This script works beautifully on my NT Intranet but after posting it to a UNIX machine (Host: 1dollarhosting.com) I get "Internal Server Error"? I have the Unix path information in the script. Did I code the script wrong for UNIX-use? Any help in getting this script to work on UNIX would be great.


(This post was edited by S_Shrum on Jan 12, 2001, 10:44 PM)


Mortimer
journeyman

Jan 13, 2001, 12:30 PM

Post #5 of 10 (769 views)
Re: How do I... User definable split character [In reply to] Can't Post

You must escape the escape...

Code
$line="abc|def|ghi"; 
$input{'seperator'}="\\|";

@fields=split(/$input{'seperator'}/,$line);

foreach(@fields){print "$_\n";}

Dave.
www.dmscripts.com
davemortimer@one.net.au




S_Shrum
User

Jan 14, 2001, 6:31 PM

Post #6 of 10 (757 views)
Re: How do I... User definable split character [In reply to] Can't Post

Nope...that didn't work. I tried passing the param like "\\|" but it split on EVERY character.

I have attached a sample.pl which is a scaled down version of the script I am writing but it has the problem area. I have also included 2 sample data files (1 pipe delimited, 1 comma delimited).

Thanx for all the help you guys are giving. I really apprechiate it.

Sean Shrum
sean@shrum.net



sleuth
Enthusiast / Moderator

Jan 15, 2001, 3:02 PM

Post #7 of 10 (748 views)
Re: How do I... User definable split character [In reply to] Can't Post

 
Are you doing this on NT or Unix/Linux?

Sleuth



japhy
Enthusiast

Jan 15, 2001, 5:56 PM

Post #8 of 10 (747 views)
Re: How do I... User definable split character [In reply to] Can't Post

Ah, the problem is thus:


Code
#!/usr/bin/perl 

$string = "this|that|those";
$sep = "\\|";
@a = split /$sep/, $string;
print "@a\n"; # this that those

print "Enter a separator: "; # enter the sequence: \\|
chomp($sep = <STDIN>);
@a = split /$sep/, $string;
print "@a\n"; # t h i s | t h a t | t h o s e

print "Enter a separator: "; # enter the character |
chomp($sep = <STDIN>);
@a = split /\Q$sep\E/, $string;
print "@a\n"; # this that those

You see, when the USER enters \\|, the split() function sees it and treats it as /\\|/, which is "a literal \ or nothing". But when you put the string \\| in a STRING IN THE PROGRAM, Perl changes the \\ to a \ right then.

The solution is to use quotemeta(), or \Q...\E, like I showed.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


S_Shrum
User

Jan 16, 2001, 2:01 PM

Post #9 of 10 (743 views)
Re: How do I... User definable split character [In reply to] Can't Post

Dev system is NT
Host system is Unix

Ultimately I would like this to work under both.

Sean Shrum
sean@shrum.net



S_Shrum
User

Jan 17, 2001, 9:49 AM

Post #10 of 10 (738 views)
Re: How do I... User definable split character [In reply to] Can't Post

Ta-Da!

The \Q...\E (quotemeta) did the trick and it works under both NT and UNIX. Kewlness. Thanx.

Sean Shrum
sean@shrum.net


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives