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



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
.



Relevant Pages

  • Re: can anyone explain the last two lines to me?thanks!
    ... That's all your loop is ... your example above this first while loop will give you the hash ... the string $_ is empty. ... element 'guans', so it returns 'hilia', etc. ...
    (comp.lang.perl.misc)
  • Re: 1 string from 3, making replacements more perlish
    ... An array of two elements would do instead of the hash. ... I'm creating string1 first,in a loop made up of X zeros and Y ones, ... This will allow me, hopefully, to process string 4. ... Otherwise, I would have to create a huge permutation first, and delete ...
    (comp.lang.perl.misc)
  • Re: Pbm in Sorting the hash
    ... start of the file string ... The data structure you want here is not a hash of strings, ... push @}, $data; ... sort takes a list not a hash, you need to say "sort keys %hash" instead. ...
    (perl.beginners)
  • Re: undif as if it is 0
    ... And the next time through the loop? ... You'll have a single hash ... > reference in @top3. ... I think I have to push 3 items and then shift and push and for that I ...
    (comp.lang.perl.misc)
  • Problem by pushing a String and a Hash on an array!
    ... I try to push on an arraya string and a hash -> ...
    (comp.lang.perl.misc)