Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Showing 'Name' not 'Full_Name' in search results?

Quote Reply
Showing 'Name' not 'Full_Name' in search results?
Hi,

I'm really banging my head on what seems like a pretty easy problem. I want to display just the non-linked category 'Name' and not the linked 'Full_Name' in the seach results. Here's where I'm at -- but it's failing miserably:

# Join the link results by category if we are grouping.
my @link_results_loop;
if ($CFG->{build_search_gb}) {
foreach my $cat (sort keys %link_output) {
my $title = Links::Build::build ('title_linked', { name => $cat, complete => 1, home => 0 });

my $catDB = $DB->table('Category');
my $cat_name = $catDB->select ('Name');

my $title_xml = $cat_name;

$link_results .= "<p>$title" . join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{$cat}});
$link_output{$cat}->[0]->{title_linked} = $title;

$link_output{$cat}->[0]->{cat_title_xml} = $title_xml;
push @link_results_loop, @{$link_output{$cat}};
}
}
else {
$link_results = join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{none}});
push @link_results_loop, @{$link_output{none}};
}

I think I'm botching it in the Select command.

Many thanks --
Mike
Quote Reply
Re: [Swaylock] Showing 'Name' not 'Full_Name' in search results? In reply to
Code:
$SUBS{build_title_unlinked} = <<'END_OF_SUB';
sub build_title_unlinked {
# --------------------------------------------------------
# Returns a string of the current category broken up by section.
# Useful for printing in the title.
#
my $input = shift;
my $output = join ": ", split (/\//, $input);

return $output;
}
END_OF_SUB
This just replaces the path separator \ or / with :

All you want to do is pick off what is to the right of the last \ or / or, if you send it to build_title_unlinked, what is to the right of the last :

I think:

my $unlinked_title = $cat;
$unlinked_title =~ s!^.*(\\|\/)!!;

might do it.

Or,
my $title = Links::Build::build ('title_unlinked', { name => $cat });
$title =~ s!^.*(:)!!


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
This just replaces the path separator \ or / with :

It only replaces / ...the \ is escaping the / to stop the regex barfing.

Quote:
All you want to do is pick off what is to the right of the last \ or / or, if you send it to build_title_unlinked, what is to the right of the last :

An optimized version would be to use rindex(), eg...

my $ending = substr($cat, rindex($cat, '/'))
Quote Reply
Re: [Paul] Showing 'Name' not 'Full_Name' in search results? In reply to
>> It only replaces / ...the \ is escaping the / to stop the regex barfing.

I was looking at my code, didn't notice that that routine only looked for the unix string.

>> my $ending = substr($cat, rindex($cat, '/'))

There's always a better way to do anything ;) I thought substr just wrapped up a regex, so actually was more cpu intensive than the regex itself, not by much, but at least one extra step. Same with rindex. I don't like regexes, and use them only when absolutely unavoidable (or already written) <G>, so your code is much easier to understand, in any case.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
No substr is far quicker than a regex. Using a regex has to load the regex engine.
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
Here's a quick benchmark...

Benchmark: timing 500,000 iterations of Paul's substr, Robert's regex...

Paul's substr:
0 wallclock secs ( 0.79 usr + 0.00 sys = 0.79 CPU) @ 632111.25/s (n=500000)

Code:
Code:
my $string = 'abc';
my $wanted = substr($string, rindex($string, 'b'));
return $wanted;


Robert's regex:
1 wallclock secs ( 1.47 usr + 0.00 sys = 1.47 CPU) @ 339673.91/s (n=500000)

Code:
Code:
my $string = 'abc';
$string =~ s!^.*(\\|/)!!;
return $string;

Last edited by:

Paul: May 15, 2003, 6:43 AM
Quote Reply
Re: [Paul] Showing 'Name' not 'Full_Name' in search results? In reply to
Thank you both for taking up my issue! I think we are almost there...

I've tried to implement Paul's solution

my $ending = substr($cat, rindex($cat, '/'));

and it works --- except for two small problems:

1. For categories right off the root (no subcategories) this rindex returns the last letter of $cat. for example "Test" will return "t"

2. For subcategories the last "/" is left on. For example "Test/2000/April" returns "/April" I like just the name and no slash.

Thanks for the help.

Mike
Quote Reply
Re: [Swaylock] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
1. For categories right off the root (no subcategories) this rindex returns the last letter of $cat. for example "Test" will return "t"

rindex() will return -1 if / can't be found, which is what will happen for root categories. Using -1 inside substr() returns the last character of the string.

Try:

Code:
my $pos = rindex($cat, '/');
my $ending = substr($cat, ($pos > -1 ? $pos + 1 : 0));
Quote Reply
Re: [Swaylock] Showing 'Name' not 'Full_Name' in search results? In reply to
did you try mine ;)

I use that regex in parsing out uploaded file names, and creating the new paths. Didn't check it closely, but it always seemed to do what I wanted, which was slice off the uploaded filename, and a leading directory separator would have caused great problems.

In my situation, 0 or 1 seconds isn't going to make a difference, but for actively displayed pages, where you might be calling the routine dozens of times a request, it might.

Of course you could always 'split' the name into an array, then call the last value as the required item:

my @cat_path = split (/\//, $cat) ;
my $cat_title = @cat_path[$#cat_path ];



PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
@cat_path[$#cat_path ];

You are trying to use an array slice there which is not what you want :)

You need:

Code:
$cat_path[-1];

Last edited by:

Paul: May 15, 2003, 12:58 PM
Quote Reply
Re: [Paul] Showing 'Name' not 'Full_Name' in search results? In reply to
Thankyou both,

I tested paul's new code:

my $pos = rindex($cat, '/');
my $ending = substr($cat, ($pos > -1 ? $pos + 1 : 0));

and indeed it works. To give Pugdog the benefit of the doubt, I went back and tested his as well and found this it too works. I think I'll skip the array test for the time being.

So now to choose...regex or not to regex...Sly

Thanks again for the help.
Quote Reply
Re: [Swaylock] Showing 'Name' not 'Full_Name' in search results? In reply to
Professionals always use regexps.

However since I saw Paul's Benchmark I'm a bit wavering. Crazy

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
Professionals always use regexps.

What gave you that idea? Sly

Experienced programmers will look for an alternative if available, especially a quicker, optimized one. For example, it's far better to use something like:

if (index($variable, '/')) {

...than....

if ($variable =~ m|/|) {

Take note of this quote:

Quote:
A regular expression (or regex) is a simple, rather mindless way of matching a series of symbols to a pattern you have in mind.

There are definitely occasions when regexs are necessary or unavoidable, but to use them "all the time" would be a huge mistake and lead to unreadable, slow code.

Last edited by:

Paul: May 16, 2003, 2:28 AM
Quote Reply
Re: [Paul] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
What gave you that idea?
Professionals told me, who knows much more than you or me...

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Showing 'Name' not 'Full_Name' in search results? In reply to
I don't wish to disappoint you, but it sounds like perhaps they don't know as much as you think.

The benchmark I did speaks for itself. It's half as CPU intensive.

Quote:
Don't use m// where substr() will do. Don't use substr() where unpack() will do. Don't use m// where index() (or even better, rindex()) will do. Don't use s/// where tr/// will do

Regexes are not the answer. They're an answer.

Last edited by:

Paul: May 16, 2003, 8:26 AM
Quote Reply
Re: [Paul] Showing 'Name' not 'Full_Name' in search results? In reply to
>> Professionals always use regexps.

Computer geeks always use regexps (look at Paul's sig line <G>). "Professionals" always use the _RIGHT_ tool for the job. That's what makes them Professionals. (Not just the paycheck.)


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
Computer geeks always use regexps (look at Paul's sig line <G>).

It doesn't use a regex Tongue
Quote Reply
Re: [pugdog] Showing 'Name' not 'Full_Name' in search results? In reply to
Quote:
Computer geeks always use regexps (look at Paul's sig line <G>)
Hehe Laugh Well, yes he wants to suggest obscurity. Laugh

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Showing 'Name' not 'Full_Name' in search results? In reply to
In Reply To:
Professionals always use regexps.

Crazy I try to avoid regexps, where convenient, to avoid the speed hit associated with them. However, I'm not going to go out of my way to come up with a way to do the same thing with 37 indexes, 12 substrs, and 6 rindexes - it would be too messy, and would probably be slower. Your state perhaps should be ammended to:

Quote:
Professionals use regexps.

Because yes, professionals must know how to use regexps, but they also need to know when _not_ to use a regexp. Just because "there's more than one way to do it" doesn't mean that all of those ways are the best, nor does it mean that any one way is going to be the best in every case. For example, given the above benchmarks Paul did, if we change the test string to actually have some /'s in it, we end up with:



Code:
[jagerman@cytherea jagerman]$ perl -MBenchmark -e 'timethese(-3, { Paul => sub {
my $c = "abc/def/ghi/jkl/mno"; my $w = substr($c, rindex($string, "b")) }, Robert => sub { my
$c = "abc/def/ghi/jkl/mno"; $c =~ s!^.*(\\|/)!!; }, Robert_modified => sub { my $c =
"abc/def/ghi/jkl/mno"; $c =~ s!^.*[\\/]!!; } })'


Benchmark: running Paul, Robert, Robert_modified for at least 3 CPU seconds...
Paul: 3 wallclock secs ( 3.13 usr + 0.00 sys = 3.13 CPU) @ 1937132.27/s (n=6063224)
Robert: 3 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 535898.73/s (n=1693440)
Robert_modified: 3 wallclock secs ( 3.15 usr + 0.00 sys = 3.15 CPU) @ 1251139.37/s (n=3941089)

(The third test modified Robert's to use [/\\] instead of (/|\\) - which is a fairly significant regexp optimization, at least when it's possible).

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com