Gossamer Forum
Home : General : Perl Programming :

problem with colon as delimiter

Quote Reply
problem with colon as delimiter
most of my tables use pipe | as delimiter but one uses colon :

this is the code that creates array from a record:

Code:
sub split_decode {

# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.


my ($input) = shift;
$input =~ s/\Q$db_delim\E$/$db_delim /o; # Add a space if we have delimiter new line.
my (@array) = split (/\Q$db_delim\E/o, $input);
for ($i = 0; $i <= $#array; $i++) {
$array[$i] =~ s/~~/$db_delim/og; # Retrieve Delimiter..
$array[$i] =~ s/``/\n/g; # Change '' back to newlines..
$array[$i] =~ s/&/&amp;/g; # encode ampersand 02/01/2017
}
return @array;
}



it works fine when $db_delim = '|' but can't find an individual record when $db_delim = ':'
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
i think this worked previously but has stopped working. (i tried to edit my message but it trashed the code)
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
Hi,

That looks a bit messy :) I would go more for something like:

Code:
sub split_decode {
# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.

my ($input) = shift;
$input =~ s/\:/__DELIM__/g; # Add a space if we have delimiter new line.
my @array = split /\:/g, $input;
foreach (@array) {
s/__DELIM__/:/g; # Convert __DELIM__ back into :
s/~~/:/g; # Retrieve Delimiter..
s/``/\n/g; # Change '' back to newlines..
s/&/&amp;/g; # encode ampersand 02/01/2017
}

return @array;
}

If $db_delim won't ever change, you may as well just hard-code it (to make things simpler). I guess it had $db_delim as it was copied from original code where this needed to be flexible.

All my new code does is convert : into __DELIM__, and then convert it back once we have done the split.

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] problem with colon as delimiter In reply to
this is the original code from dbman. most of the tables use pipe but i'm also reading an apache-compatible login db which uses the colon. documentation says i should be able to use the colon as a delimiter.
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
Firstly, get rid of /o on the regex ... its useless and has been obsolete for years :)

$db_delim

What value does that have if you print it out to debug? It's hard to know what you are doing - but my sample code should work fine. Print out the data being passed along as well. The thing with these kinds of things, is debug debug debug. Add as much "print" (or STDERR if you have access to "tail" the error log to keep track of things), and then see if its what you are expecting

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] problem with colon as delimiter In reply to
when i tell it to print $db_delim it prints the colon. i've printed everything that makes sense! i'm going to test something directly in the user table and see what happens.
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
ok, i tested searching for delicia directly in the user file and it worked fine. when i'm in another table and want to find the related record (one-to-one) in the related table, it doesn't work. the code i posted earlier for split_decode is for currently active table. i modified it to search in related table:

Code:
sub split_decode2 {
# --------------------------------------------------------
# Takes one line of the database as input and returns an
# array of all the values. It replaces special mark up that
# join_encode makes such as replacing the '``' symbol with a
# newline and the '~~' symbol with a database delimeter.


my ($input) = shift;
$input =~ s/\Q$db2_delim\E$/$db2_delim /o; # Add a space if we have delimiter new line.
my (@array) = split (/\Q$db2_delim\E/o, $input);
for ($i = 0; $i <= $#array; $i++) {
$array[$i] =~ s/~~/$db2_delim/og; # Retrieve Delimiter..
$array[$i] =~ s/``/\n/g; # Change '' back to newlines..
$array[$i] =~ s/&/&amp;/g; # encode ampersand 02/01/2017
}
return @array;
}

the only thing i changed in this sub is $db_delim to $db2_delim. the code that switches to related table uses $db2_delim.
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
It's really hard to test without some actual data files and then putting it on my own server and testing it out (just a sample .db file that has the issue, and then the current code you are using to try and extract it)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
BTW how is split_decode() actually called? If it per line? Or on the whole file?

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
This test script works perfectly for me:

Code:
my $data = q|2819:delicia:Reynolds Delicia:18.4:20151015:Yes::zzzzz|;

my @foo = split_decode($data);

use Data::Dumper;
print Dumper(@foo);

sub split_decode {

my $db2_delim = ":"; # manually set this here as obviously I'm not using dbman

my ($input) = shift;
$input =~ s/\Q$db2_delim\E$/$db2_delim /o; # Add a space if we have delimiter new line.
my (@array) = split (/\Q$db2_delim\E/o, $input);
for ($i = 0; $i <= $#array; $i++) {
$array[$i] =~ s/~~/$db2_delim/og; # Retrieve Delimiter..
$array[$i] =~ s/``/\n/g; # Change '' back to newlines..
$array[$i] =~ s/&/&amp;/g; # encode ampersand 02/01/2017
}
return @array;

}

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] problem with colon as delimiter In reply to
split decode is called for each line and then the comparison to the key is made.
Quote Reply
Re: [Andy] problem with colon as delimiter In reply to
when you say it works perfectly, does dumper show that exact line with the delimiters?
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
Hi,

It split fine for me. This is what it gave me with your data:

Code:
perl test.cgi
$VAR1 = '2819';
$VAR2 = 'delicia';
$VAR3 = 'Reynolds Delicia';
$VAR4 = '18.4';
$VAR5 = '20151015';
$VAR6 = 'Yes';
$VAR7 = '';
$VAR8 = 'zzzzz';

This is where "use strict" really comes in handy. You most likely have a typo somewhere that isn't being picked up due to "strict" not being enabled (no point trying to turn it on in your script though, as it has tons and tons of violations and stuff that would cause errors if you did ;))

Apart from actually testing it myself and seeing the issue happening, its hard to really debug any further (afraid I won't be able to help today though, as I'm about to go and cook a BBQ for 5 people =))

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] problem with colon as delimiter In reply to
thanks. i have tested using the original code for get_record and split_decode and it works. i can't remember why i created modified routines before. i guess i was afraid that overriding the default settings for the current db would cause problems. but if i switch back to the original settings immediately after retrieving relational data, maybe everything would be ok?

Code:

sub switch_to_testuserxxx { not working with get_record2 and split_decode2
#-----------------------------------------------------
undef @db_cols;
undef @db2_cols;
$configfile = "handicap.def";
$configfile = $db_data_path . "/" . $configfile;
&get_fieldnames2($configfile);
@db_cols = @db2_cols;
$db2_file_name = $db_data_path . "/testuser.db";
$db2_key_pos = '1';
$db2_key = 'Userid';
$db2_delim = ':';
$db2_id_file_name = $db_data_path . "/handicap.count";
}




Code:
sub switch_to_testuser { # working with get_record and split_decode
#-----------------------------------------------------
undef @db_cols;
undef @db2_cols;
$configfile = "handicap.def";
$configfile = $db_data_path . "/" . $configfile;
&get_fieldnames2($configfile);
@db_cols = @db2_cols;
$db_file_name = $db_data_path . "/testuser.db";
$db_key_pos = '1';
$db_key = 'Userid';
$db_delim = ':';
$db_id_file_name = $db_data_path . "/handicap.count";
}

if i have similar code to switch back to current, do you think it would work?
Quote Reply
Re: [delicia] problem with colon as delimiter In reply to
Sorry I don't know enough about DB Man to how it works. This is why I prefer mySQL as you don't have these problems :)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!