recursive function and hashe

P

phaylon

Sébastien Cottalorda said:
I think I need to use recursive function, but I did know how, I'm not
familiar with passing references.

Yep. That would be one way to do it. Some other way I can imagine includes
two Loops.
If someone can help me.

Sure, many here in do. But, you see, there are /many/ people hanging at
problems, wanting help. So it would be much more useful for us (because it
is easier) and you (because more ppl are going to read it, and much more
are going to answer it) if you try out yourself and ask for help with the
problems you're running in.


p
 
S

Sébastien Cottalorda

I all,

Here is my problem,

I'd like to store in a hash table those numbers like that:
(123, 124, 13, 145, 2, 25)

%number = (
"1" => {
"2" => {
"3" => {
"value" => "OK"
},
"4" => {
"value" => "OK"
},
},
"3" => {
"value" => "OK"
},
"4" => {
"5" => {
"value" => "OK"
}
}
},
"2" => {
"value" => "OK",
"5" => {
"value" => "OK"
}
}
);

I think I need to use recursive function, but I did know how, I'm not
familiar with passing references.

If someone can help me.

Thanks in advance.

Sébastien
 
T

Tad McClellan

Sébastien Cottalorda said:
I all,

Here is my problem,

I'd like to store in a hash table those numbers like that:
(123, 124, 13, 145, 2, 25)

%number = (
"1" => {
"2" => {
"3" => {
"value" => "OK"
},
"4" => {
"value" => "OK"
},
},
"3" => {
"value" => "OK"
},
"4" => {
"5" => {
"value" => "OK"
}
}
},
"2" => {
"value" => "OK",
"5" => {
"value" => "OK"
}
}
);

I think I need to use recursive function, but I did know how,


---------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my %number;
foreach my $num ( 123, 124, 13, 145, 2, 25 ) {
add_to_hash( \%number, $num );
}
print Dumper \%number;


sub add_to_hash {
my($h, $num) = @_;

if ( $num =~ s/^(\d)// ) {
$h->{$1} = {} unless exists $h->{$1};
add_to_hash($h->{$1}, $num);
}
else {
$h->{value} = 'OK';
}
}
 
B

Brian McCauley

Sébastien Cottalorda said:
I all,

Here is my problem,

I'd like to store in a hash table those numbers like that:
(123, 124, 13, 145, 2, 25)

%number = (
"1" => {
"2" => {
"3" => {
"value" => "OK"
},
"4" => {
"value" => "OK"
},
},
"3" => {
"value" => "OK"
},
"4" => {
"5" => {
"value" => "OK"
}
}
},
"2" => {
"value" => "OK",
"5" => {
"value" => "OK"
}
}
);

That's a very odd structure - what are you planning to use it for?
I think I need to use recursive function,

Whilst a recursive function may seeme the most natural approach acually
an iterative one works perfectly well in this case.

for (123, 124, 13, 145, 2, 25) {
my $r = \\%number;
$r = \$$r->{$_} for split //;
$$r->{value} = 'OK';
}
but I did know how, I'm not familiar with passing references.

The only way to become familiar is to read documentation and try.
 
A

Anno Siegel

Brian McCauley said:
That's a very odd structure - what are you planning to use it for?

It's a (variant of a) trie structure -- not entirely unheard of, but
rarely used for numeric data like this.

Anno
 
S

Sébastien Cottalorda

Brian said:
Sébastien Cottalorda said:
I all,

Here is my problem,

I'd like to store in a hash table those numbers like that:
(123, 124, 13, 145, 2, 25)

%number = ( [snip]
"2" => {
"value" => "OK",
"5" => {
"value" => "OK"
}
}
);

That's a very odd structure - what are you planning to use it for?

I'm planning to make a program that allow me to determine from which country
is a Credit Card.
I've downloaded the BIN table that is basically like this:
MinCC-MaxCC Network(VISA,MASTER,NATIONAL,etc...) Country
The problem is that each interval do not contains the same number of Credit
Card.
ie:
1234000000000000-1234999999999999 VISA USA
1235678900000000-1235678999999999 VISA CHN
2567894561000000-2567894561999999 MASTER TUR
etc...

I then plan to store in memory each interval:
- 1234
- 12356789
- 2567894561
as seen before.
When I try to determine from which country a Credit Card is, I use a
recursive algorithme.
For example: from which country is the credit card number 1234567890123456 ?
1 -> 2 -> 3 -> 4 -> (value=>'OK') ----> Country Found (If, instead of "OK",
I put the country, I can know it).
Another example: from wich country is the credit card number
1239123456789456 ?
1 -> 2 -> 3 -> 9 -> (do not exists) ----> "Country not found"

I hope the BIN Table is not too big.
Whilst a recursive function may seeme the most natural approach acually
an iterative one works perfectly well in this case.

for (123, 124, 13, 145, 2, 25) {
my $r = \\%number;
$r = \$$r->{$_} for split //;
$$r->{value} = 'OK';
}


The only way to become familiar is to read documentation and try.


I agree with that, but I need to quick process the BIN table file, I didn't
have time to read the documentation on this precise problem.
I'd rather ask a very precise question hoping someone helps me.
Note, I didn't ask for my entire problem, I only ask for the number to hash
transformation.

Cheers.

Sébastien
 
A

Anno Siegel

Sébastien Cottalorda said:
Brian said:
Sébastien Cottalorda said:
I all,

Here is my problem,

I'd like to store in a hash table those numbers like that:
(123, 124, 13, 145, 2, 25)

%number = ( [snip]
"2" => {
"value" => "OK",
"5" => {
"value" => "OK"
}
}
);

That's a very odd structure - what are you planning to use it for?

I'm planning to make a program that allow me to determine from which country
is a Credit Card.
I've downloaded the BIN table that is basically like this:
MinCC-MaxCC Network(VISA,MASTER,NATIONAL,etc...) Country
The problem is that each interval do not contains the same number of Credit
Card.
ie:
1234000000000000-1234999999999999 VISA USA
1235678900000000-1235678999999999 VISA CHN
2567894561000000-2567894561999999 MASTER TUR
etc...

[...]

Look on CPAN for ready-made solutions. Tie::RangeHash looks good, there
may be others.

Anno
 
S

Sébastien Cottalorda

Anno Siegel wrote:

[snip]
I'm planning to make a program that allow me to determine from which
country is a Credit Card.
I've downloaded the BIN table that is basically like this:
MinCC-MaxCC Network(VISA,MASTER,NATIONAL,etc...) Country
The problem is that each interval do not contains the same number of
Credit Card.
ie:
1234000000000000-1234999999999999 VISA USA
1235678900000000-1235678999999999 VISA CHN
2567894561000000-2567894561999999 MASTER TUR
etc...

[...]

Look on CPAN for ready-made solutions. Tie::RangeHash looks good, there
may be others.

Anno

Thanks I've modified my program for Tie::RangeHash, it seems to work exactly
as I need.
Once the BIN table loaded, my program as 33 Mo size, but it answers quickly.
The recursive method I've implemented works either, but the code is bigger.

Thanks again Anno.

Sébastien
 
N

nobull

Sébastien Cottalorda said:
Thanks I've modified my program for Tie::RangeHash

Tie::RangeHash was also the solution I was going to mention.

[ Ealier in this thread Sébastien had accidently replied to me
personally rather than the group and I said I would recommend a CPAN
module but I wouldn't say which one unless he responded to me in the
group :) ]
 

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

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top