Ask a Question related to PERL Modules, Design and Development.
-
Steffen Müller #1
Re: hash generation question
Stephan wrote:
[...]How's this related to the modules list?> For example, suppose I have this:
>
> @list1 = qw(x1 x2 x3 data);
>
> How can I generate an hash like
>
> hash{x1}{x2}{x3} = data ?
>
> The only thing I could come up with is to generate the above line and
> eval it but I'd guess there must be a more clean way to deal with
> this?
Anyhow, what about this:
my $hashref = \%hash;
$hashref = $hashref->{$_} foreach @list1[0..$#list1-2];
$hashref->{$list1[-2]} = $list1[-1];
Steffen
--
*_=*DATA;seek+_,0,0;print<_>;
__DATA__
What's a quine?
Steffen Müller Guest
-
Simple Hash Question
Hi .... I have a hash defined as my %flag3 = ( A => "DBSpace backup thread", B => "Begin work", C => "Commiting/committed", H => "Heuristic... -
[PHP-DEV] hash table question
For my upcoming improvement of interbase.c, which features asynchronous handling of events posted in the database, I need to maintain a per-link... -
Another reference question (hash of hash references)
beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes...... -
Proposal: Array#to_h, to simplify hash generation
Hi -talk, Ruby has wonderful support for chewing and spitting arrays. For instance, it's easy to produce an array from any Enumerable using... -
Big hash question
I have a very large hash inside a script. The $values of the $keys can be very large. When the script runs, it takes a long time to load the... -
Harald Joerg #2
Re: hash generation question
Steffen Müller <sv99oya02@sneakemail.com> writes:
Did you test this loop?> Stephan wrote:
> [...]>> > For example, suppose I have this:
> > @list1 = qw(x1 x2 x3 data);
> > How can I generate an hash like
> > hash{x1}{x2}{x3} = data ?
> > The only thing I could come up with is to generate the above line and
> > eval it but I'd guess there must be a more clean way to deal with
> > this?
> How's this related to the modules list?
>
> Anyhow, what about this:
>
> my $hashref = \%hash;
> $hashref = $hashref->{$_} foreach @list1[0..$#list1-2];
What would you expect to happen if %hash is empty initially?
$hashref->{$_} is undef in this case, and all iterations keep
setting $hashref to an undefined value.
In short: It does not work at all.
--
Cheers,
haj
Harald Joerg Guest
-
Harald Joerg #3
Re: hash generation question
Stephan <stephanj@NOSPAMsci.kun.nl> writes:
The "1" in the variable name suggests that you would like to merge more> I'm trying to build a hash structure on the fly without knowledge about
> depts in my program. (aka userinput)
>
> For example, suppose I have this:
>
> @list1 = qw(x1 x2 x3 data);
than one list in one hash. Is that correct?
After reviewing my code below, I thought: How ugly. Perhaps eval isn't> How can I generate an hash like
>
> hash{x1}{x2}{x3} = data ?
>
> The only thing I could come up with is to generate the above line and eval
> it but I'd guess there must be a more clean way to deal with this?
that bad for the problem.
Then, on second thought: If you use eval, you might want to be prepared
for crafted user input which tries to break your program's security.
There is no easy way to eval strings containing user input.
So you might want to give it a try....
----------------------------------------------------------------------
#!perl -w
use strict;
use Data::Dumper;
my @lists = ([qw(x1 x2 x3 data)]
,[qw(x1 x2 prunes_previous_hash)]
,[qw(tooshort)]
,[qw(y1 dada)]
,[qw(y1 replaces scalar by hash)]
,[undef,undef,'undef gives warnings']
);
my %hash = ();
for my $list (@lists) {
my $r_h = \%hash;
unless (scalar @$list >= 2) {
warn "skipping a list with less than two elements";
next;
}
my ($last,$value) = splice @$list,-2;
for (@$list) {
$r_h->{$_} = {} unless (ref $r_h->{$_} eq 'HASH');
$r_h = $r_h->{$_};
}
$r_h->{$last} = $value;
}
print Dumper(\%hash);
----------------------------------------------------------------------
--
Good luck,
haj
Harald Joerg Guest
-
Steffen Müller #4
Re: hash generation question
Harald Joerg wrote:
>>my $hashref = \%hash;
>>$hashref = $hashref->{$_} foreach @list1[0..$#list1-2];No, obviously. So make it:> Did you test this loop?
my $hashref = \%hash;
foreach (@list1[0..$#list1-2]) {
$hashref->{$_} = {} unless ref($hashref->{$_}) eq 'HASH';
$hashref = $hashref->{$_};
}
$hashref->{$list1[-2]} = $list1[-1];
Again. Untested. Sue me.
--
@n=([283488072,6076],[2105905181,8583184],[1823729722,9282996],[281232,
1312416],[1823790605,791604],[2104676663,884944]);$b=6;@c=' -/\_|'=~/./g
;for(@n){for$n(@$_){map{$h=int$n/$b**$_;$n-=$b**$_*$h;$c[@c]=$h}reverse
0..11;push@p,map{$c[$_]}@c[reverse$b..$#c];$#c=$b-1}$p[@p]="\n"}print@p;
Steffen Müller Guest
-
Jimbo@Jimbo.com #5
Re: hash generation question
In article <bdmdt1$lt7$1@wnnews.sci.kun.nl>
Stephan <stephanj@NOSPAMsci.kun.nl> wrote:
Recursion can be your friend:>>>>>> @list1 = qw(x1 x2 x3 data);
>> The "1" in the variable name suggests that you would like to merge more
>> than one list in one hash. Is that correct?
>>>>> How can I generate an hash like
>>>
>>> hash{x1}{x2}{x3} = data ?
>>>
>>> The only thing I could come up with is to generate the above line and eval
>>> it but I'd guess there must be a more clean way to deal with this?
> Totally correct, so (x1 x2 x4 data) etc in the same hash.>>>
>> After reviewing my code below, I thought: How ugly. Perhaps eval isn't
>> that bad for the problem.
>>
>> Then, on second thought: If you use eval, you might want to be prepared
>> for crafted user input which tries to break your program's security.
>> There is no easy way to eval strings containing user input.
> The ", \ etc problems. I've escaped around those 2. The userinput is my own,
> so I can avoid most trapholes.
>>>> my ($last,$value) = splice @$list,-2;
>> for (@$list) {
>> $r_h->{$_} = {} unless (ref $r_h->{$_} eq 'HASH');
>> $r_h = $r_h->{$_};
>> }
>> $r_h->{$last} = $value;
>> }
> This seems to work like expected. Just funny that such a simple problem
> doesn't really have a clean, nice perl way.
>
> Stephan
my @list1 = qw(x1 x2 x3 data);
my $hash = build( 0, \@list1 );
sub build {
my ( $i, $list ) = @_;
my $key = $list->[ $i++ ];
return { $key => $i < 3 ? build( $i, $list ) : $list->[$i] };
}
print $$hash{x1}{x2}{x3};
Jimbo.
Jimbo@Jimbo.com Guest
-
Benjamin Goldberg #6
Re: hash generation question
Stephan wrote:
[snip]What's so unsimple about:> This seems to work like expected. Just funny that such a simple
> problem doesn't really have a clean, nice perl way.
my $ref = \\%hash;
$ref = \$$ref->{$_} for @list1[0 .. $#list-1];
$$ref = $list[-1];
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
Benjamin Goldberg Guest



Reply With Quote

