hashes

J

John Carroll

I believe this is a seriously newbie question, but I can't find
anything that gives me the correct result. :^)

How do you get the actual string value of a hash value, when the key
is known? I am using the following:

%in is set using the &ReadParse from cgi-lib.pl, with "Password" being
one of the keys, with a value of "test".

$file_password = "test";
$password = $in{"Password"};
print $password; # output is "test"
print $file_password; # output is "test"
if ($password ne $file_password) { # result is true
$incorrect_pw = "true";
}

I know about "each", "keys", and "values", but I can't find anything
in O'Reilly's "Programming Perl 2nd Edition" that shows me how to get
the value of a hash when you know the key using these commands.

If anyone has any ideas, I would appreciate them greatly.
Please send email to (e-mail address removed) with any help. I will also
check back on this list until Friday.

thanks,
john
 
J

Jürgen Exner

John said:
How do you get the actual string value of a hash value, when the key
is known?

Do you want to the value or do you want the value as a string? Those may be
two different requests.
%in is set using the &ReadParse from cgi-lib.pl, with "Password" being
one of the keys, with a value of "test".

$file_password = "test";
$password = $in{"Password"};

You are doing it right there.
$password now contains the value of the hash %in for the key 'Password'.

If you want to make sure that the value is a string (why do you care?) then
just concatenate the value with an empty string.
Please send email to (e-mail address removed) with any help.

Don't think so, this is not how Usenet works.
I will also check back on this list until Friday.

The answer will still be waiting for you.

jue
 
T

Tore Aursand

%in is set using the &ReadParse from cgi-lib.pl

Why don't you use CGI.pm instead? I don't know the current status of the
'cgi-lib.pl' thing, but as far as I know we're talking about code which
isn't updated anymore.
$file_password = "test";
$password = $in{"Password"};

That would be ...

my $cgi = CGI->new();
my $file_password = 'test';
my $password = $cgi->param( 'Password' ) || '';

.... if you're using CGI.pm and strict.
print $password; # output is "test"
print $file_password; # output is "test"
if ($password ne $file_password) { # result is true
$incorrect_pw = "true";
}

Remember that there's not 'true' value in Perl. Personally, I stick with
setting variables to 1 or 0, and just testing them;

my @bool = qw( 1 0 );
foreach ( @bool ) {
if ( $_ ) {
print "True\n";
}
else {
print "False\n";
}
}
I know about "each", "keys", and "values", but I can't find anything
in O'Reilly's "Programming Perl 2nd Edition" that shows me how to get
the value of a hash when you know the key using these commands.

You must have missed a part, if I'm not misunderstanding you completely.
Have a look at this:

my %hash = (key_1 => 'value 1',
key_2 => 'value 2');
print $hash{ 'key_1' } . "\n";

What do you think? Will this print 'value 1' or 'value 2'? :)
Please send email to (e-mail address removed) with any help.

Can't do that. This is Usenet.
 
J

John Carroll

Jürgen Exner said:
Do you want to the value or do you want the value as a string? Those may be
two different requests.

I don't care what type it is, but I am comparing it to a string, so I
assume it has to be a string for the comparison to work properly.
You are doing it right there.
$password now contains the value of the hash %in for the key 'Password'.

If you want to make sure that the value is a string (why do you care?) then
just concatenate the value with an empty string.

I don't care, but my comparison to a string doesn't work. I
concatenated it with an empty string and I still get the same answer
as originally, which was that if I compare two strings that print
identically, the comparison fails (returns not equal). Also, if I set
both variables to strings explicitly, or even only explicitly reset
the variable that originally holds the value of the hash %in for the
key 'Password', I get the comparison to work properly, but if I leave
it as shown above, the comparison fails.

*snip*

thanks,
john
 
J

J. Gleixner

John said:
I don't care what type it is, but I am comparing it to a string, so I
assume it has to be a string for the comparison to work properly.




I don't care, but my comparison to a string doesn't work. I
concatenated it with an empty string and I still get the same answer
as originally, which was that if I compare two strings that print
identically, the comparison fails (returns not equal). Also, if I set
both variables to strings explicitly, or even only explicitly reset
the variable that originally holds the value of the hash %in for the
key 'Password', I get the comparison to work properly, but if I leave
it as shown above, the comparison fails.

Short example..

my $file_password = "test";
my $password = $in{"Password"};
my %in;
$in{"Password"} = "test";
$in{"Password2"} = $file_password;
print "1. they're equal\n" if $file_password eq $in{"Password"};
print "2. they're equal\n" if $file_password eq $password;
print "3. they're still equal\n" if $file_password eq $in{"Password2"};

1. they're equal
2. they're equal
3. they're still equal


Maybe the values you're comparing aren't what you think they are? Maybe
one contains a " "? Try:

print "-$password-\n";
print "-$in{Password}-\n";

So you can see if there are any blanks in the beginning/end.
 
J

Jay Tilton

(e-mail address removed) (John Carroll) wrote:

: but my comparison to a string doesn't work. I
: concatenated it with an empty string and I still get the same answer
: as originally, which was that if I compare two strings that print
: identically,

Could there be leading or trailing whitespace in either string? That
could cause them to _appear_ to be identical without being identical.
Surrounding the printed string with visible characters can reveal the
presence of whitespace.

$file_password = "test";
$password = $in{"Password"};
print "-->$password<--";
print "-->$file_password<--";
if ($password ne $file_password) {
$incorrect_pw = "true";
}
 
J

John Carroll

(e-mail address removed) (John Carroll) wrote:

: but my comparison to a string doesn't work. I
: concatenated it with an empty string and I still get the same answer
: as originally, which was that if I compare two strings that print
: identically,

Could there be leading or trailing whitespace in either string? That
could cause them to _appear_ to be identical without being identical.
Surrounding the printed string with visible characters can reveal the
presence of whitespace.

I already thought of that, but thanks for the idea. I happen to use
the vertical bar "|", but anything works. :^)
 
J

John Carroll

*snip to save space*
Short example..

my $file_password = "test";
my $password = $in{"Password"};
my %in;
$in{"Password"} = "test";
$in{"Password2"} = $file_password;
print "1. they're equal\n" if $file_password eq $in{"Password"};
print "2. they're equal\n" if $file_password eq $password;
print "3. they're still equal\n" if $file_password eq $in{"Password2"};

1. they're equal
2. they're equal
3. they're still equal

I'll try all of these, to see if I get any different answers.
Maybe the values you're comparing aren't what you think they are? Maybe
one contains a " "? Try:

print "-$password-\n";
print "-$in{Password}-\n";

So you can see if there are any blanks in the beginning/end.

See my earlier post... I already tried this, and got identical output.
Oddly, if I do a cmp on the string, I get a -1 result, which means my
hash value variable is less than the test variable.

thanks,
john
 
J

John Carroll

Well, after much testing, I have another puzzler (I've kinda given up
on the previous problem, and moved on)...

BTW, I switched to using CGI.pm, in case that was my problem. And
note that "testing" is what is sent as the password from the web page,
using POST method.

Following one of the previous posts, I do the following:

Code:
$cgi = CGI->new ();
$file_password = "testing";
$password = $cgi->param ("Password");
print "file password: |$file_password|\n";
print "password: |$password|\n";
print $cgi->param('Password');
print "\n";
print "$cgi->param('Password')";
print "\n";

Result:
file_password: |testing|
password: ||
testing
CGI=HASH(0xf296c)->param('Password')

Why does adding double quotes to the print of the hash result change
the output? And why does $password not seem to get set to the result
of the hash, or get set to an unprintable value (I think these
questions are related)?

thanks,
john
 
M

Michael Budash

Well, after much testing, I have another puzzler (I've kinda given up
on the previous problem, and moved on)...

BTW, I switched to using CGI.pm, in case that was my problem. And
note that "testing" is what is sent as the password from the web page,
using POST method.

Following one of the previous posts, I do the following:

Code:
$cgi = CGI->new ();
$file_password = "testing";
$password = $cgi->param ("Password");
print "file password: |$file_password|\n";
print "password: |$password|\n";
print $cgi->param('Password');
print "\n";
print "$cgi->param('Password')";
print "\n";

Result:
file_password: |testing|
password: ||
testing
CGI=HASH(0xf296c)->param('Password')

Why does adding double quotes to the print of the hash result change
the output?

the double quotes around $cgi->param('Password') cause '$cgi' to be
interpolated (correctly), but not 'param'. this is to be expected:
variables are interpolated, not methods.
And why does $password not seem to get set to the result
of the hash, or get set to an unprintable value (I think these
questions are related)?

dunno... works for me...
 
T

Tad McClellan

John Carroll said:
print "$cgi->param('Password')";
CGI=HASH(0xf296c)->param('Password')

Why does adding double quotes to the print of the hash result change
the output?


You can interpolate variables.

You cannot interpolate function (method) calls, without resorting
to the trickery described in this Perl FAQ:

How do I expand function calls in a string?
 
T

Tad McClellan

John Carroll said:
$cgi = CGI->new ();
$file_password = "testing";
$password = $cgi->param ("Password");
print "file password: |$file_password|\n";
print "password: |$password|\n";
print $cgi->param('Password');
print "\n";
print "$cgi->param('Password')";
print "\n";

And why does $password not seem to get set to the result
of the hash,


What hash?
 
C

Charles DeRykus

...
$cgi = CGI->new ();
$file_password = "testing";
$password = $cgi->param ("Password");
print "file password: |$file_password|\n";
print "password: |$password|\n";
print $cgi->param('Password');
print "\n";
print "$cgi->param('Password')";
print "\n";

Result:
file_password: |testing|
password: ||
testing
CGI=HASH(0xf296c)->param('Password')

Why does adding double quotes to the print of the hash result change
the output? And why does $password not seem to get set to the result
of the hash, or get set to an unprintable value (I think these
questions are related)?

Are you sure a Password value is set...

../tmp.pl Password=testing
file password: |testing|
password: |testing|
testing
CGI=HASH(0x23204)->param('Password')

hth,
 
T

Tore Aursand

$cgi = CGI->new ();
$file_password = "testing";
$password = $cgi->param ("Password");

You really should 'use strict', too;

my $cgi = CGI->new();
my $file_password = 'testing';
my $password = $cgi->param( 'Password' ) || '';
print "file password: |$file_password|\n";
print "password: |$password|\n";
print $cgi->param('Password');
print "\n";
print "$cgi->param('Password')";
print "\n";

Watch out for double quotes and what they do; They'll try to interpolate
what's inside them. I prefer to write things like this (thus making use
of the syntax highlighting in my editor);

print 'file password: |' . $file_password . '|' . "\n";
...
print $cgi->param( 'Password' ) . "\n";

Also remember that you can have a look at all the parameters:

my @params = $cgi->param();
foreach my $param ( @params ) {
print $param . ' = |' . $cgi->param( $param ) . '|' . "\n";
}

Nice for debugging etc.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,360
Latest member
kathdev

Latest Threads

Top