Solved

comparing "split" strings of an array with next element

Posted on 1998-11-09
13
263 Views
Last Modified: 2010-03-04
Hi!
I was wondering if there was an easier method of comparing two substrings on two separate lines. I am retrieving data from a database, in which I "join" 6 pieces of data of a record with a ":", and push it into an array. Once all data has been extracted, I return the joined-string array to main. The main function, "splits" the data into it's individual strings. How do I, after splitting the data, compare 3 of the 6 pieces of data with the same 3 strings of the next element in the array.
0
Comment
Question by:bcoballe
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:thoellri
ID: 1206056
@ary=('abc:def:123:456:ghi:789', 'ABC:def:123:456:GHI:789');
$res=(split(/:/,$ary[0]))[1,3,4] eq (split(/:/,$ary[1]))[1,3,4];
print $res,"\n";
$res=(split(/:/,$ary[0]))[1,2,5] eq (split(/:/,$ary[1]))[1,2,5];
print $res,"\n";

Is this what you're looking for?

Hope this helps
  Tobias

0
 
LVL 84

Expert Comment

by:ozo
ID: 1206057
#on the other hand
@ary=('abc:def:123:456:ghi:789', 'RST:UVW:987:654:XYZ:789');
$res=(split(/:/,$ary[0]))[1,2,5] eq (split(/:/,$ary[1]))[1,2,5];
print $res,"\n";
#may not be what you're looking for
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206058
Whoops : thoellri turned himself into a bozo ... what was I htinking then?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206059
#perhaps you were thinking of
$res=join(':',(split(/:/,$ary[0]))[1,2,5]) eq join(':', (split(/:/,$ary[1]))[1,2,5]);
#but I'm more curious what is bcoballe thinking of?
0
 

Expert Comment

by:mazares
ID: 1206060
Use an associate %array instead of a @list. Then you can do something like this:

($a1, $b1, $c1, $d1, $e1, $f1) = split(/:/, $array{$rec1});
($a2, $b2, $c2, $d2, $e2, $f2) = split(/:/, $array{$rec2});
if (($a1 eq $a2) && ($c1 eq $c2) && ($e1 eq $e2)) {
    print "All three records match\n";
}


0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206061
mazares:
What's the difference between
      split(/:/,$array{$rec1})
and
      split(/:/,$array[$rec1])
?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 1206062
$array[$rec1] expects $rec1 to be an integer, whereas $array{$rec1} allows $rec1 to be an arbitrary string.
But that no bearing on what happens with the split
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206063
thoellri made himself a bozo again: ozo - I knew about the difference, i just don't see how using an assoc would make this problem any easier ...

0
 
LVL 5

Expert Comment

by:thoellri
ID: 1206064
And by the way - who says it has to be an integer?:

@ary=("One", "Two", "Three");
print $ary["a"],"\n";

Now let's see how many people are completly confused :-)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1206065
Unless there's something else about the problem that we haven't been told, using an assoc seems to do nothing to make the problem any easier

"a" == "0"
0
 

Author Comment

by:bcoballe
ID: 1206066
Thanks for your help everyone! The following, from Ozo is exactly what I was looking for. Works beautifully!

bcoballe

#perhaps you were thinking of
$res=join(':',(split(/:/,$ary[0]))[1,2,5]) eq  join(':',(split(/:/,$ary[1]))[1,2,5]);
#but I'm more curious what is bcoballe thinking of?



0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1206067
($a1, $c1, $e1) = (split/:/, $array[$rec1])[0,2,4];
($a2, $c2, $e2) = (split/:/, $array[$rec2])[0,2,4];
if( ($a1 eq $a2) && ($c1 eq $c2) && ($e1 eq $e2) ){
         print "All three records match\n";
}
#also works
0
 

Author Comment

by:bcoballe
ID: 1206068
The proposed answer is what I was originally doing. I wanted to know if there was a shorter method. The following is more what I had in mind, also from ozo:

#perhaps you were thinking of
$key=join(':',(split(/:/,$ary[0]))[1,2,5]) eq  join(':',(split(/:/,$ary[1]))[1,2,5]);
#but I'm more curious what is bcoballe thinking of?

However, I couldn't get the equating of the the two "joins" to work. Splitting and joining each row separately works. Once done that, I compared the two keys.

Since I have never seen this done before "join(':',(split(/:/,$ary[1]))[1,2,5]);", I was really impressed with this answer. So, this is what I have done:

$key1 = join(':',(split(/:/,$ary[i}))[1,2.5]);
$key2 = join(':',(split(/:/,$ary[i+1]))[1,2,5]);

if($key1 eq $key2) {



}

So, On these regards, giving the answer "B" - because the thought process is excellent, but the equate doesn't work.

Thanks again, bcoballe
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 22
Perl Sort Question 4 129
Exchange 2010 Transport Rule Regex 28 95
Perl Untar File 1 27
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now