Re: How to get a computed string to act as a re in if statement
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Wed, 30 Jul 2008 15:02:59 +0100
Mike Martin wrote:
I am working on the sub below.
The aim is to create a string from values passed to the sub-routine
and then use that as a if criterion for the push operation.
Is this possible?
(Code reformatted for legibility)
use strict;
use warnings;
my %guide;
get_list(undef, undef, {a => 1, b => 2, c => 3});
sub get_list {
my ($black_list, $table, $crit) = @_;
# $crit is a hash reference containing key to search and search
# expression, for example start=Jul 07
#
my $listings;
# This is a loop through hash containing key, value pairs of field,
# value and placing into an array
#
my @crit;
foreach my $keys (keys %{$crit}){
push @crit,'{'."'".$keys."'".'}=~'.$crit->{$keys};
}
my $crit1 = join ' and ', @crit; ### array to string ###
### This is the record definition from a master hash ###
###loop start ###
foreach my $guides (sort {
$guide{$a}->{'start'} <=> $guide{$b}->{'start'}
} keys %guide) {
push @{$listings}, [
$guide{$guides}->{'id'},
$guide{$guides}->{'Channel'},
substr($guide{$guides}->{'start1'},0,16),
$guide{$guides}->{'start'},
substr($guide{$guides}->{'stop1'},0,16),
$guide{$guides}->{'stop'},
$guide{$guides}->{'name'},
$guide{$guides}->{'cat0'}
];
# here I want an if statement to run that is generated depending on what
# keys are in the oringinal %crit hash. Number of arguments variable
# if $crit1;
# this is the problem rather than searching on the value of
# $guide{$guides}-> it prints it out as a literal
}
}
I'm not sure what you're trying to do here. The code is very unclear, so
I've run your code with a call of
get_list(undef, undef, {a => 1, b => 2, c => 3});
and I've found that for this input you're building a string in $crit1 of
{'c'}=~3 and {'a'}=~1 and {'b'}=~2
which isn't valid Perl (well it's syntactically correct, but you're applying
regular expressions to anonymous hashes with a single entry, which is nonsense.
Can you explain a little better what it is that you're trying to do please? Then
we may be able to help you. It looks like you're trying to write Perl code at
run time, for which you need eval(), but there are almost always better ways to
do things than to use eval.
Rob
.
- References:
- How to get a computed string to act as a re in if statement
- From: Mike Martin
- How to get a computed string to act as a re in if statement
- Prev by Date: Re: Regular expression: How to determine wether entry is a number?
- Next by Date: Re: Question about lwp-rget
- Previous by thread: How to get a computed string to act as a re in if statement
- Next by thread: Re: How to get a computed string to act as a re in if statement
- Index(es):
Relevant Pages
|