Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Search an Array Question

by rogermills (Initiate)
on Nov 10, 2009 at 04:24 UTC ( [id://806117]=perlquestion: print w/replies, xml ) Need Help??

rogermills has asked for the wisdom of the Perl Monks concerning the following question:

Monks I have array that looks like this (output of a sql query).
16410 88 bhorwat, dpotte1, jbehr, lgrimes 16412 88 pputta1 16413 88 gkothi, aandy

Where the first number is a unique bug id number and the names are all the developers that are assigned the bug id. I want to search the bug id and check if the developer is assigned the bug. I was able to get the bug id out as it is unique. But if i search for the developer then one developer maybe assigned multiple bugs so this where i am stuck. I want if the developer is assigned the same bug

What i want to do is search for the bug id and if the developer is assigned the same bug then compare to the developer who is commiting the code. e.g. commiter is gkothi and is assigned bug 16413 (which is equal to strippeddirectory). this is what i have
#!D:\perl\bin\perl use strict; #use warnings; use DBI; use List::Util qw(first); # Replace datasource_name with the name of your data source. # Replace database_username and database_password # with the SQL Server database username and password. my $data_source = q/dbi:ODBC:test/; my $user = q/tracker/; my $password = q/TrAck3r/; #Absolute path to the svnlook program my $svnlook = "D:\\Subversion\\bin\\svnlook.exe"; my $debugEnabled = 0; my $argNum = 0; my ($repo_path, $txn_name) = @ARGV; my $teamtrack = "sa_svndev"; ## Check to see if debug is enabled foreach $argNum (0 .. $#ARGV) { if ( ($ARGV[$argNum] eq "-d") || ($ARGV[$argNum] eq "-D")) { $debugEnabled = 1; } } if ($debugEnabled) { print "\n------ DEBUG IS ENABLED ------\n"; ## Use revision numbers since there is not a transaction number $txn_name = "-r $txn_name"; } else { ## Use transaction numbers $txn_name = "-t $txn_name"; } my $committer = `$svnlook author $txn_name $repo_path` or die("Unable to get committer with svnlook.\n"); chomp($committer); if ($committer eq $teamtrack ) { print "Not Checking since user is teamtrack"; exit 0; } #print "\n\n$committer"; my $changeddirectory = `$svnlook dirs-changed $txn_name $repo_path` or die("Unable to get the changed directories with svnlook.\n"); #print "\n\nDirectory changed $changeddirectory"; my $strippeddirectory = $1 if ($changeddirectory =~ /(\d+)/); #print "$strippeddirectory"; # Connect to the data source and get a handle for that connection. my $dbh = DBI->connect($data_source, $user, $password) or die "Can't connect to $data_source: $DBI::errstr"; # This query generates a result set with one record in it. my $sql = "select ucs.ts_issueid as CNR_NUMBER, ucs.ts_state as CNR_STATE, LTRIM((SELECT STUFF((SELECT ', ' + List FROM (SELECT List = substring((SELECT ( ', ' + TS_LOGINID ) FROM dbo.TS_USERS t2 WHERE t1.val = t2.TS_ID + FOR XML PATH( '' ) ), 3, 1000 )FROM (SELECT * FROM dbo.Split(uc +s.ts_developers_ext, ',') WHERE VAL <> '') t1) as subquery FOR XML PATH('')), 1, 1, ''))) as DEVELOPERS from usr_cnr_solution ucs inner join ts_states s on s.ts_id = ucs.ts_state where s.ts_id = 88 "; # Prepare the statement. my $sth = $dbh->prepare($sql) or die "Can't prepare statement: $DBI::errstr"; # Execute the statement. $sth->execute(); # Fetch and display the result set value. while ( my @row = ($sth->fetchrow_array )) { print "@row\n"; my $cnrissue = first { /$strippeddirectory/ } @row; my $cnrdev = first { /$committer/ } @row; print "$cnrissue"; print "$cnrdev"; } # Disconnect the database from the database handle. $dbh->disconnect;
Any help is greatly appreciated

Replies are listed 'Best First'.
Re: Search an Array Question
by colwellj (Monk) on Nov 10, 2009 at 05:18 UTC
    So to simplify lets ignore the database for now and say you have - a bug id, a committers name and an array containing the bug id, a code and a list of developers involved in the code
    I assume that you don't want someone who is not assigned the bug committing the code but the reverse is just as easy.
    #!/usr/bin/perl -w use strict; my $bug_no = shift; my $committer = shift; my @array =("16410 88 bhorwat, dpotte1, jbehr, lgrimes", "16412 88 pputta1", "16413 88 gkothi, aandy"); my %hash; foreach(@array){ #remove those commas s/,//g; #get your bug id and a list of devs my ($bug_id,undef,@dev) = split/\s+/,$_; foreach(@dev){ #dont really need data in the hash only the key $hash{$bug_id}{$_} = $_; } } #this can be a loop if you have many bugs and committer combos if(!defined($hash{$bug_no}{$committer})){ print "Committer $committer is not assigned to Bug $bug_no"; }
Re: Search an Array Question
by bobf (Monsignor) on Nov 10, 2009 at 04:34 UTC

    I'd be happy to try to help, but honestly I'm not sure what you are looking for. Please include some sample data and expected output that can be used to verify solutions to your problem.

    Also, it's generally not a good idea to hard code authentication credentials into your script (hint, hint). Reading from the command line or accessing an environment variable would be better.

      My bad Here is the data
      15485 88 dbaril1, sthiaga, vgamji1 15500 88 jyoung1 15502 88 deure, sciftci
      What i want is if bug ID 15485 is found store the whole line in a string. The output should be like this with only the name not the number
      dbaril1, sthiaga, vgamji1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://806117]
Approved by bobf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-23 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found