A
Avinash Magar
Can somebody please me tell the way to write this script in Ruby?
;---------------------------------------
#!/bin/env perl
use strict;
use Data:umper;
#; define some message templates here
my $message_templates = [
'adm has _QTY_ _CAR_ cars',
'ssn had _QTY_ _CAR_ cars',
];
#; value map
my %value_map = (
'_CAR_' => {
'order' => 'rand',
'vals' => [qw(bentley merc bmw audi porsche ford honda lexus)]
},
'_QTY_' => {
'order' => 'rand',
'vals' => [1..10]
}
);
# using 'random_msg_stub and val generators from %value_map
sub get_next_random_msg {
my ($next_msg, $val_map) = @_;
my $m = &$next_msg();
while (my($vk, $v) = each (%value_map)) {
my ($nval) = $v->{gen}->();
$m =~ s/$vk/$nval/g;
}
$m;
}
# return $arrya[$last + 1] elem of array; $last is remembered across
invocations
sub get_seq_val_generator {
my $a = $_[0];
my $last = 0;
return sub {
$last = 0 if ($last == scalar @$a);
$$a[$last++];
};
}
# return random elem of array
sub get_random_val_generator {
my $arr = shift;
return sub { return $arr->[ rand @$arr] };
}
#; let's generate value generator foreach value in 'value_map
while (my($vk, $v) = each (%value_map)) {
if ($v->{order} eq 'seq') {
$v->{gen} = get_seq_val_generator($v->{vals});
} elsif ($v->{order} eq 'rand') {
$v->{gen} = get_random_val_generator($v->{vals});
} else {
die "Invalid value order in value_map\n";
}
}
my ($get_next_random_msg_stub) =
get_random_val_generator($message_templates);
while (1) {
print get_next_random_msg($get_next_random_msg_stub, \%value_map),
"\n\n";
}
1;
;--------------------------------------------------
thanks in advance.
;---------------------------------------
#!/bin/env perl
use strict;
use Data:umper;
#; define some message templates here
my $message_templates = [
'adm has _QTY_ _CAR_ cars',
'ssn had _QTY_ _CAR_ cars',
];
#; value map
my %value_map = (
'_CAR_' => {
'order' => 'rand',
'vals' => [qw(bentley merc bmw audi porsche ford honda lexus)]
},
'_QTY_' => {
'order' => 'rand',
'vals' => [1..10]
}
);
# using 'random_msg_stub and val generators from %value_map
sub get_next_random_msg {
my ($next_msg, $val_map) = @_;
my $m = &$next_msg();
while (my($vk, $v) = each (%value_map)) {
my ($nval) = $v->{gen}->();
$m =~ s/$vk/$nval/g;
}
$m;
}
# return $arrya[$last + 1] elem of array; $last is remembered across
invocations
sub get_seq_val_generator {
my $a = $_[0];
my $last = 0;
return sub {
$last = 0 if ($last == scalar @$a);
$$a[$last++];
};
}
# return random elem of array
sub get_random_val_generator {
my $arr = shift;
return sub { return $arr->[ rand @$arr] };
}
#; let's generate value generator foreach value in 'value_map
while (my($vk, $v) = each (%value_map)) {
if ($v->{order} eq 'seq') {
$v->{gen} = get_seq_val_generator($v->{vals});
} elsif ($v->{order} eq 'rand') {
$v->{gen} = get_random_val_generator($v->{vals});
} else {
die "Invalid value order in value_map\n";
}
}
my ($get_next_random_msg_stub) =
get_random_val_generator($message_templates);
while (1) {
print get_next_random_msg($get_next_random_msg_stub, \%value_map),
"\n\n";
}
1;
;--------------------------------------------------
thanks in advance.