Re: How to get a computed string to act as a re in if statement
- From: jwkrahn@xxxxxxx (John W. Krahn)
- Date: Wed, 30 Jul 2008 13:25:53 -0700
Mike Martin wrote:
Hi
Hello,
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?
[ Whitespace adjusted for clarity. ]
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 ###
So 'start' is the key and 'Jul 07' is the value?
my $listings;
#### This is a loop through hash containing key, value pairs of field,
#### value and placing into an array ####
foreach my $keys ( keys %{ $crit } ) {
push @crit, '{' . "'" . $keys . "'" . '}=~' . $crit->{ $keys };
Why the braces around $keys? So you want to create the string (for example) "{'start'}=~Jul 07"? That doesn't make much sense. Perhaps you meant to use the %guide hash with the $keys key?
}
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
} #### loop end ###
}
If my assumptions are correct then this may be what you want:
sub get_list {
my ( $black_list, $table, $crit ) = @_;
my %flags;
for my $key ( keys %$crit ) {
$flags{ $key } = $guide{ $key } =~ $crit->{ $keys };
}
my $listings;
for my $guides ( sort { $guide{ $a }{ start } <=> $guide{ $b }{ start } } keys %guide ) {
push @$listings, [
@{ $guide{ $guides } }{ qw/id Channel/ },
substr( $guide{ $guides }{ start1 }, 0, 16 ),
$guide{ $guides }{ start },
substr( $guide{ $guides }{ stop1 }, 0, 16 ),
@{ $guide{ $guides } }{ qw/stop name cat0/ },
]
if $flags{ $guides };
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.
- 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: XS
- Next by Date: Re: Evaluate an operator given as a string
- Previous by thread: Re: How to get a computed string to act as a re in if statement
- Next by thread: Evaluate an operator given as a string
- Index(es):
Relevant Pages
|