Re: How to get a computed string to act as a re in if statement



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
.



Relevant Pages

  • Re: something like switch in c
    ... >> straightforward string comparisions. ... > inner table size and/or add symbols to expand the hash. ... It all depends on the empirical pattern of the actual keys. ... The value of the random number generator is UNCHANGED on ...
    (comp.programming)
  • Re: Hash keys
    ... hash with a symbol is not the same as using a string. ... symbols for my keys. ... YAML object into a hash, and didn't realize it was keyed with strings so ... What is the preferred method of keying hashes? ...
    (comp.lang.ruby)
  • Re: Merging hashes using both symbols and strings as keys
    ... default values for my class) and the other using strings as keys ... (taken from the params hash). ... I made a small module called SymbolizeKeys that will allow you to extend ... # converts any current string keys to symbol keys ...
    (comp.lang.ruby)
  • Re: help with regex
    ... I'm using the %data hash to store information about each ... Each start hash has two keys, ... data structures can be used, particularly for the last level, but I ... sub print_start ...
    (comp.lang.perl.misc)
  • Re: Merging hashes using both symbols and strings as keys
    ... default values for my class) and the other using strings as keys ... (taken from the params hash). ... the string, ... # the returned hash will also be extended by SymbolizeKeys ...
    (comp.lang.ruby)