Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / July 2008



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Martin - 30 Jul 2008 11:08 GMT
Hi

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?

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 ####

    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

} #### loop end ###

}

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Rob Dixon - 30 Jul 2008 15:02 GMT
> 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

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

John W. Krahn - 30 Jul 2008 21:25 GMT
> 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;
>
[quoted text clipped - 3 lines]
>     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 ###
[quoted text clipped - 24 lines]
>     } #### 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
Signature

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.