Looping in Perl based on defined keys in Map


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping in Perl based on defined keys in Map
# 1  
Old 06-24-2013
Looping in Perl based on defined keys in Map

Hello All,

I am writing the below script where it will connect to database and returns the results.


Code:
#!/sw/gcm/perl510/bin/perl
use SybaseC;
 
&openConnection;
&loadvalues;
 
sub openConnection {
  $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD});
  if ($dbproc->status() ne  $SybaseC::OK) {
    &exitProc(-1, "Connection to SYDB NOT established");
  }
}
 
my ( @ssq ) = (
        "select *
        from flex..test_book a, flex1..test_book1
        where a.date = b.date
        and $variable  <-- I need to run the SQL mutliple times for diffent conditions.
        and a.emp_name = b.emp_name
 
        select row_count = \@\@rowcount"
);
 
my $re = $dbproc->Returh(\@ssq);
if ( @$re ) {
    foreach my $has ( @$res ) {
      if ( $$has{emp_no} ne "" ) {
        $NEW_Empl{"NEW_Emp_no"}{"$$has{emp_no}|$$has{id}"}++;
      }
      $row_count = $$has{row_count};
if ( $row_count ne "" && $row_count == 0 ) {
        print STDERR "No new records";
      }
    }
  } else {
    print STDERR "Error";
  }
}

As indicates above in Red, I need to run the sql multiple times based on the map defined i.e. ($variable)

Code:
%map = (
flag = 'Y'
exlcuse_id =2
..
so on
)

here, $variable has to pickup each value from the %map defined and run the sql.

Could someone please suggest me in writing the looping over statement.

Really appreciate your efforts and time.
# 2  
Old 06-25-2013
how is the %map actually set and where is it in relation to the other items in your script?
# 3  
Old 06-28-2013
Quote:
Originally Posted by filter
...
Code:
...
my ( @ssq ) = (
        "select *
        from flex..test_book a, flex1..test_book1
        where a.date = b.date
        and $variable  <-- I need to run the SQL mutliple times for diffent conditions.
        and a.emp_name = b.emp_name
 
        select row_count = \@\@rowcount"
);
...

As indicates above in Red, I need to run the sql multiple times based on the map defined i.e. ($variable)

Code:
%map = (
flag = 'Y'
exlcuse_id =2
..
so on
)

...
Instead of connecting to the database, executing the query and fetching the resultsets multiple times for all those conditions, you could combine the conditions together with the "OR" operator and fire your query only once.

That way you perform the tasks - connection, execution and fetch only once.

Here's some sample code to combine the conditions in the hash -

Code:
$
$ cat -n combine_conditions.pl
     1  #!/usr/bin/perl -w
     2  use strict;
     3  my $conditions;                                  # scalar that holds the combination of all conditions
     4  my %map = qw (flag 'Y' exclude_id 2 name 'ABC'); # hash that has conditions as key-value pairs
     5  while (my ($k,$v) = each %map) {                 # loop through each key-value pair
     6    $conditions .= "or $k = $v "                   # and keep appending the conditions
     7  }                                                # till we are done; hash iteration returns the pairs in no particular order
     8  $conditions =~ s/^or (.*) $/($1)/;               # remove the "or " at the front, the space at the end and wrap around with parentheses
     9  print $conditions, "\n";                         # $conditions can now be plugged in the query, right after AND operator
$
$ perl combine_conditions.pl
(name = 'ABC' or exclude_id = 2 or flag = 'Y')
$
$


Last edited by durden_tyler; 06-30-2013 at 05:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find All duplicates based on multiple keys

Hi All, Input.txt 123,ABC,XYZ1,A01,IND,I68,IND,NN 123,ABC,XYZ1,A01,IND,I67,IND,NN 998,SGR,St,R834,scot,R834,scot,NN 985,SGR0399,St,R180,T15,R180,T1,YY 985,SGR0399,St,R180,T15,R180,T1,NN 985,SGR0399,St,R180,T15,R180,T1,NN 2943,SGR?99,St,R68,Scot,R77,Scot,YY... (2 Replies)
Discussion started by: unme
2 Replies

2. Shell Programming and Scripting

Help Dynamic looping based on files

Hi I have to run the script (a part of the code) in a loop for the no of times the files present in the directory, by taking one file and process and next another file. For example, if we do ls and the result have: $ls abc.dat def.dat ghi.dat The script code should loop for 3... (4 Replies)
Discussion started by: karumudi7
4 Replies

3. Shell Programming and Scripting

Grep and map in perl

Hi guys, I'm trying to learn grep and map and having a little problem. Let's say I have a file which contains: Apple: abcdcabdadddbac I want to replace any combinations of three of abcd, thus when I do this: print grep {s/{3}/X/g} <F>; # will do the subtitution fine, output XXXX ... (1 Reply)
Discussion started by: new bie
1 Replies

4. Shell Programming and Scripting

printing keys only using map

Hello everyone, Can anyone give me a suggestion on lines below: %hash = map {($_, -M "$dir/$_")} readdir D; print map "$_\n", sort values %hash; The thing is this will print the floating point numbers of files recently created/modified. I'd like to print out the file names only while... (0 Replies)
Discussion started by: new bie
0 Replies

5. Shell Programming and Scripting

Compare columns of 2 files based on condition defined in a different file

I have a control file which tells me which are the fields in the files I need to compare and based on the values I need to print the exact value if key =Y and output is Y , or if output is Y/N then I need to print only Y if it matches or N if it does not match and if output =N , then skip the feild... (7 Replies)
Discussion started by: newtoawk
7 Replies

6. Shell Programming and Scripting

Sum a column value based on multiple keys

Hi, I have below as i/p file: 5ABC 36488989 K 000010000ASB BYTRES 5PQR 45757754 K 000200005KPC HGTRET 5ABC 36488989 K 000045000ASB HGTRET 5GTH 36488989 K 000200200ASB BYTRES 5FTU ... (2 Replies)
Discussion started by: nirnkv
2 Replies

7. Shell Programming and Scripting

Perl map doubt

Hello , Please can someone tell me what exactly happens when the below filehandler is chomped into an array and later mapped. $lcpLog="logcopy\@".getTimestamp."\log"; open CFg ,"< $lcpcfg"; chomp(@cfg = <CFG>); close CFG; @cfg=grep { $_ ne ' ' } map { lc + (split /\s*\/\//) }... (0 Replies)
Discussion started by: rmv
0 Replies

8. Shell Programming and Scripting

select values based on keys

HI The input 1st column has specific keys like 1 with value a,b and c. 2 with b,b,d and 3 with a,a a. when ever c appears as one of the values the result will be key ........ c (You can see in the out put as 1 w...... 6.... c) and same follows for d. Thanx:) I'm learning awk scripting. If... (3 Replies)
Discussion started by: repinementer
3 Replies

9. UNIX for Dummies Questions & Answers

Joining files based on multiple keys

I need a script (perl or awk..anything is fine) to join 3 files based on three key columns. The no of non-key columns can vary in each file. The columns are delimited by semicolon. For example, File1 Dim1;Dim2;Dim3;Fact1;Fact2;Fact3;Fact4;Fact5 ---- data delimited by semicolon --- ... (1 Reply)
Discussion started by: Sebben
1 Replies

10. Programming

marge tow files based on keys

how can i marge two files depend som key for example: the first file include many records of information for X person and the second file have one record of information for each X person shortly i want to mak first :match between the two files then insert data from the second to the first... (2 Replies)
Discussion started by: Ehab
2 Replies
Login or Register to Ask a Question