Idiot Q: How to find index number of HASH match?

F

Franzl Wisseworst

Mumia W. wrote:

[..]
my $match_index = $obj_en->Indices($current::page);
my $string = defined $match_index ? $match_index : "404.html";
[..]

my $string = $obj_de->Keys($match_index);

Another (stupid) question: I presume by the above code the string
"404.html" is meant to be returned in case $current::page has no match in
the tied hash array. But if the page does not exist my $string always
defaults to index 1 of the other array for some reason. In other words: ...

print $string;

.... returns the match in the corresponding hash array correctly as it
should if a match is found, while if no matching $current::page is found,
$match_index always contains an empty string. Not the "404.html" string.

It is no problem to get around it as follows: ...

if ($match_index eq ""){
print "404.html";
}
else {
print $string;
}

.... but I don't think that was the general idea. Perhaps it's something in
the line:
my $string = defined $match_index ? $match_index : "404.html";

Or maybe I'm handling what's returned in a wrong way? In any case, the
right result is there in a roundabout way.
 
A

Alan J. Flavell

Mumia W. wrote: ...

Another (stupid) question: I presume by the above code the string
"404.html" is meant to be returned in case $current::page has no match in
the tied hash array. But if the page does not exist my $string always
defaults to index 1 of the other array for some reason.

If 404.html is supposed to be in reference to the HTTP return code for
"page not found", then there does seem to be a problem with this
(unless there's some machinery elsewhere that I haven't seen).

Returning a page called 404.html (presumably with an HTTP return code
200 = OK) is a weak apology for sending a real HTTP 404 return code,
to tell every client (browser, indexing bot or whatever) that the page
really doesn't exist.

Slightly off-topic for this group, I admit, but I thought it needed
mentioning.
 
D

David Squire

Alan said:
If 404.html is supposed to be in reference to the HTTP return code for
"page not found", then there does seem to be a problem with this
(unless there's some machinery elsewhere that I haven't seen).

I'm pretty sure this was just supposed to be an illustrative example...
Returning a page called 404.html (presumably with an HTTP return code
200 = OK) is a weak apology for sending a real HTTP 404 return code,
to tell every client (browser, indexing bot or whatever) that the page
really doesn't exist.

Especially since putting this:

ErrorDocument 404 /some/appropriate/path/404.html

in a .htaccess file or server config file will do just what is wanted.


DS
 
F

Franzl Wisseworst

David Squire wrote:

[..]
I'm pretty sure this was just supposed to be an illustrative example...

Yes a somewhat misleading example, in fact, it is meant to be a mirror when
a page does not exist in the other language, with a notice such as:
print "This page does currently not yet exist in $opposite_language";
while it is presumably under construction/translation.
 
M

Mumia W.

[...]
It is no problem to get around it as follows: ...

if ($match_index eq ""){
print "404.html";
}
else {
print $string;
}

.... but I don't think that was the general idea. Perhaps it's something in
the line:
my $string = defined $match_index ? $match_index : "404.html";

Or maybe I'm handling what's returned in a wrong way? In any case, the
right result is there in a roundabout way.

The documentation on my system doesn't say what
Tie::Hash->Indices() returns on failure, so I assumed it
returned undef.

The line I wrote above says "if $match_index is defined,
return $match_index, otherwise return '404.html'". Now I want
it to say, "if $match_index is not the empty string, return
$match_index, otherwise return '404.html'".

my $string = $match_index ne '' ? $match_index : '404.html';


Look at "perldoc perlop" § _Conditional Operator_ if the "?
.... : ..." syntax confuses you.


HTH
 
F

Franzl Wisseworst

Mumia W. wrote:

[..]
The line I wrote above says "if $match_index is defined,
return $match_index, otherwise return '404.html'". Now I want
it to say, "if $match_index is not the empty string, return
$match_index, otherwise return '404.html'".

my $string = $match_index ne '' ? $match_index : '404.html';

Thanks for explaining and providing the above example.
Look at "perldoc perlop" § _Conditional Operator_ if the "?
... : ..." syntax confuses you.

I'm reading the 32-page docs now, which should make me a bit wiser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top