Thread: @array =~ regex

    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    50

    @array =~ regex


    I think I am trying to do something that Perl doesn't want me to do.

    I'm trying to do an if condition test the inside of an @array for a value and if true execute.

    Maybe Perl doesn't like my syntax.
    Code:
    if (@row =~ /$in{'discount_ID'}/) {
    @row ends up populated with:
    Code:
    @row = 21 22 23 24 25 26 27 28 29 30
    $in{'discount_ID'} is populated with:
    Code:
    $in{'discount_ID'} = 21 or any number in @row
    How should I be doing this?

    Code:
    	 my $r_discntcodes = readdata("$pubdiscnt_data") || my_error("Can't open $pubdiscnt_data",$!);
    	 my $fields = shift @{$r_discntcodes};
             chomp($fields);
             my @fields = split(/\|/,$fields);
             
             foreach (@$r_discntcodes) {
    	   $html_output .= qq~
    	   <tr bgcolor="$bg_color{'1'}" align="left">~;        	
    	   chomp;
    	   my @row = split /\|/;
    	   my $j = -1;
    	   if (@row =~ /$in{'discount_ID'}/) {
                  foreach (@row) {
    	         $j++;
    	         $record{$fields{$j}} = $_;
                     $html_output .= qq~
                     <th bgcolor="$bgcolor">
                     <font face="$font_face{'1'}" size="$font_size{'1'}"
                     color="$font_color{'1'}">$record{$fields{$j}}</font></th>~;
                  }
               }
            }
    Thanks for any insight.

    Wes
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,273
    Rep Power
    1813
    I would recommend using the smart match operator as described in perlfaq4, or convert the row array to a hash and ask if the element exists.

    Comments on this post

    • additude agrees : Thanks!
    • Will-O-The-Wisp agrees
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    50
    Thanks again keath,

    Following the link and doing some research I ended up with this working solution.

    Oh, and learned something in the process.
    Code:
    	 my $r_discntcodes = readdata("$pubdiscnt_data") || my_error("Can't open $pubdiscnt_data",$!);
    	 my $fields = shift @{$r_discntcodes};
             chomp($fields);
             my @fields = split(/\|/,$fields);
             
             foreach (@$r_discntcodes) {
    	   $html_output .= qq~
    	   <tr bgcolor="$bg_color{'1'}" align="left">~;        	
    	   chomp;
    	   my @row = split /\|/;
    	   my $j = -1;
    		my %discnt_check = map { $_ => 1 } @row;
    		if(exists($discnt_check{$in{'discount_ID'}})) {
                  foreach (@row) {
    	         $j++;
    	         $record{$fields{$j}} = $_;
                     $html_output .= qq~
                     <th bgcolor="$bgcolor">
                     <font face="$font_face{'1'}" size="$font_size{'1'}"
                     color="$font_color{'1'}">$record{$fields{$j}}</font></th>~;
                  }
               }
            }

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for posting your solution!
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Posts
    509
    Rep Power
    50
    Closed the Thread

IMN logo majestic logo threadwatch logo seochat tools logo