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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to