I have a program:
use strict ;
use DB_File ;
my %h ;
sub Compare
{
my ($key1, $key2) =
@_ ;
"L$key1" cmp "L$key2" ;
}
# specify the Perl sub that will do the comparison
#$DB_BTREE->{'compare'} = &Compare ;
tie %h, "DB_File", "astdb", O_RDWR, 0640, $DB_BTREE
or die "Cannot open file 'tree': $!
" ;
# Cycle through the keys printing them in order.
# Note it is not necessary to sort the keys as
# the btree will have kept them in order automatically.
foreach (keys %h)
{
#print "$_=$h{$_}
";
}
untie %h ;
The program reads through the database and prints the key and the corresponding value.
First of all I would be very very grateful if someone can explain what this program does. I have a rough idea. I have very little knowledge of perl I have used php a lot.
What i want to be able to do is given a key print out the corresponding value. This program is printing all the values but i want to print specific value. For example there is a key /08845241830/8339 i want to print out its specific value. Ive tried so many things it just doesnt work. Can someone help?
Comments
: I have a program:
:
: use strict ;
Says we want Perl to be anal...uh...I mean, helpful about dubious code. :-)
: use DB_File ;
:
This loads the DB_File handler.
: my %h ;
:
Declare a hash named h. A hash is an associative array - it has key/value pairs. For example:-
my %language_opinions = (
perl => 'rocks',
php => 'sucks'
); # Joke, joke...
But when you access a single value in a hash by it's key, you're just getting one value. So for example we'd do:-
print "I think perl $language_opinions{'perl'}.
";
: sub Compare
: {
: my ($key1, $key2) = @_ ;
: "L$key1" cmp "L$key2" ;
: }
:
Declares a sub called compare. It takes two parameters (parameters come in the @_ array) and does a case insensitive compare (L = lowercase what follows).
: # specify the Perl sub that will do the comparison
: #$DB_BTREE->{'compare'} = &Compare ;
:
You don't want to comment that out. :-) $DB_BTREE is a variable that is exported by DB_File. On this line you override the standard compare routine for a B-tree (a tree format highly suited to on-disk storage) with the one you define in the sub Compare. & is the sigil for subs, makes a reference so &Compare gives you a reference to compare (e.g. you aren't calling the sub, just a reference to it so it can be called later). $DB_TREE turns out to be a reference to a hash (it may actually be an object based around a hash, but we don't really care). -> works like in PHP for calling methods on an object.
: tie %h, "DB_File", "astdb", O_RDWR, 0640, $DB_BTREE
: or die "Cannot open file 'tree': $!
" ;
:
Tied variables are a very cool idea...that you won't find in many other languages. Here we "tie" the hash %h with the class DB_File, passing it some initial parameters. The class DB_File then over-rides the basic operations you can do on a hash, such as getting the keys, getting/setting values, etc. In this case it over-rides them with code that stores the values in a file. But to the programmer it feels like they're just manipulating any old hash.
: # Cycle through the keys printing them in order.
: # Note it is not necessary to sort the keys as
: # the btree will have kept them in order automatically.
:
:
: foreach (keys %h)
That's practically all English, not Perl. :-)
: { #print "$_=$h{$_}
";
: }
$_ is the default variable. So each key in %h is assigned to $_ and key by key the loop is executed. So here we're just printing (or would be if you hadn't commented it out) key=value.
: untie %h ;
:
Here we clear up after ourselves.
: First of all I would be very very grateful if someone can explain what this program does. I have a rough idea. I have very little knowledge of perl I have used php a lot.
:
Perl is kinda like turbo-charged PHP. Harder to learn, but a lot more expressive. Oh, and it has interesting features like tied variables.
: What i want to be able to do is given a key print out the corresponding value. This program is printing all the values but i want to print specific value. For example there is a key /08845241830/8339 i want to print out its specific value. Ive tried so many things it just doesnt work. Can someone help?
:
Replace the loop at the end with something like:-
print "Enter key: ";
my $key = ; # Read a line from the console
chop $key; # Get rid of the newline character we read
print $h{$key}; # Print the value for that key
Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
: : I have a program:
: :
: : use strict ;
: Says we want Perl to be anal...uh...I mean, helpful about dubious code. :-)
:
: : use DB_File ;
: :
: This loads the DB_File handler.
:
: : my %h ;
: :
: Declare a hash named h. A hash is an associative array - it has key/value pairs. For example:-
:
: my %language_opinions = (
: perl => 'rocks',
: php => 'sucks'
: ); # Joke, joke...
:
: But when you access a single value in a hash by it's key, you're just getting one value. So for example we'd do:-
:
: print "I think perl $language_opinions{'perl'}.
";
:
: : sub Compare
: : {
: : my ($key1, $key2) = @_ ;
: : "L$key1" cmp "L$key2" ;
: : }
: :
: Declares a sub called compare. It takes two parameters (parameters come in the @_ array) and does a case insensitive compare (L = lowercase what follows).
:
: : # specify the Perl sub that will do the comparison
: : #$DB_BTREE->{'compare'} = &Compare ;
: :
: You don't want to comment that out. :-) $DB_BTREE is a variable that is exported by DB_File. On this line you override the standard compare routine for a B-tree (a tree format highly suited to on-disk storage) with the one you define in the sub Compare. & is the sigil for subs, makes a reference so &Compare gives you a reference to compare (e.g. you aren't calling the sub, just a reference to it so it can be called later). $DB_TREE turns out to be a reference to a hash (it may actually be an object based around a hash, but we don't really care). -> works like in PHP for calling methods on an object.
:
: : tie %h, "DB_File", "astdb", O_RDWR, 0640, $DB_BTREE
: : or die "Cannot open file 'tree': $!
" ;
: :
: Tied variables are a very cool idea...that you won't find in many other languages. Here we "tie" the hash %h with the class DB_File, passing it some initial parameters. The class DB_File then over-rides the basic operations you can do on a hash, such as getting the keys, getting/setting values, etc. In this case it over-rides them with code that stores the values in a file. But to the programmer it feels like they're just manipulating any old hash.
:
: : # Cycle through the keys printing them in order.
: : # Note it is not necessary to sort the keys as
: : # the btree will have kept them in order automatically.
: :
: :
: : foreach (keys %h)
: That's practically all English, not Perl. :-)
:
: : { #print "$_=$h{$_}
";
: : }
: $_ is the default variable. So each key in %h is assigned to $_ and key by key the loop is executed. So here we're just printing (or would be if you hadn't commented it out) key=value.
:
: : untie %h ;
: :
: Here we clear up after ourselves.
:
: : First of all I would be very very grateful if someone can explain what this program does. I have a rough idea. I have very little knowledge of perl I have used php a lot.
: :
: Perl is kinda like turbo-charged PHP. Harder to learn, but a lot more expressive. Oh, and it has interesting features like tied variables.
:
: : What i want to be able to do is given a key print out the corresponding value. This program is printing all the values but i want to print specific value. For example there is a key /08845241830/8339 i want to print out its specific value. Ive tried so many things it just doesnt work. Can someone help?
: :
: Replace the loop at the end with something like:-
:
: print "Enter key: ";
: my $key = ; # Read a line from the console
: chop $key; # Get rid of the newline character we read
: print $h{$key}; # Print the value for that key
:
: Jonathan
:
: ###
: for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
: (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
: /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
:
:
Thank you very much for your reply. I found it very helpful. I actually did try something similar to your suggestion before but it didnt work. I tried your code like this:
use strict ;
use DB_File ;
my %h ;
sub Compare
{
my ($key1, $key2) = @_ ;
"L$key1" cmp "L$key2" ;
}
# specify the Perl sub that will do the comparison
$DB_BTREE->{'compare'} = &Compare ;
tie %h, "DB_File", "astdb", O_RDWR, 0640, $DB_BTREE
or die "Cannot open file 'tree': $!
" ;
# Add a key/value pair to the file
#$h{''} = 'Larry' ;
#$h{'Smith'} = 'John' ;
#$h{'mouse'} = 'mickey' ;
#$h{'duck'} = 'donald' ;
# Delete
#delete $h{"duck"} ;
# Cycle through the keys printing them in order.
# Note it is not necessary to sort the keys as
# the btree will have kept them in order automatically.
print "Enter key: ";
my $key = ; # Read a line from the console
chop $key; # Get rid of the newline character we read
print "Value: $h{$key}";
untie %h ;
When prompted I entered: /08845241830/8339
The output was value:
In other words null or nothing. Now the program I initially posted had loads of output from the database one such output was:
/08845241830/8339=0845241
So as you can see from the initial program it does return a value for the key /08845241830/8339 but in the second program it returns nothing. This is the problem i faced yesterday as i tried method after method to extract the value but I kept on getting nothing.
Would appreciate if you can point out where i have gone wrong on this.
thanks.
:
: use strict ;
: use DB_File ;
:
: my %h ;
:
: sub Compare
: {
: my ($key1, $key2) = @_ ;
: "L$key1" cmp "L$key2" ;
: }
:
: # specify the Perl sub that will do the comparison
: $DB_BTREE->{'compare'} = &Compare ;
:
: tie %h, "DB_File", "astdb", O_RDWR, 0640, $DB_BTREE
: or die "Cannot open file 'tree': $!
" ;
:
: # Add a key/value pair to the file
: #$h{''} = 'Larry' ;
: #$h{'Smith'} = 'John' ;
: #$h{'mouse'} = 'mickey' ;
: #$h{'duck'} = 'donald' ;
:
: # Delete
: #delete $h{"duck"} ;
:
: # Cycle through the keys printing them in order.
: # Note it is not necessary to sort the keys as
: # the btree will have kept them in order automatically.
:
: print "Enter key: ";
: my $key = ; # Read a line from the console
: chop $key; # Get rid of the newline character we read
: print "Value: $h{$key}";
:
: untie %h ;
:
: When prompted I entered: /08845241830/8339
:
: The output was value:
:
: In other words null or nothing. Now the program I initially posted had loads of output from the database one such output was:
:
: /08845241830/8339=0845241
:
: So as you can see from the initial program it does return a value for the key /08845241830/8339 but in the second program it returns nothing. This is the problem i faced yesterday as i tried method after method to extract the value but I kept on getting nothing.
:
: Would appreciate if you can point out where i have gone wrong on this.
:
I can't actually see anything wrong with what you've done there and would have expected it to work. Can you change:-
print "Value: $h{$key}";
To
print "$key = $h{$key}";
Just to confirm it is seeing the input of the key OK. Also add "use warnings;" below "use strict;" to see if it gives us any useful warnings.
Jonathan
###
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");