• Status: Solved
• Priority: Medium
• Security: Public
• Views: 291

# comparing "split" strings of an array with next element

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
bcoballe
• 5
• 5
• 2
• +1
1 Solution

Commented:
@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

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

Commented:
Whoops : thoellri turned himself into a bozo ... what was I htinking then?
0

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

Commented:
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

Commented:
mazares:
What's the difference between
split(/:/,\$array{\$rec1})
and
split(/:/,\$array[\$rec1])
?

0

Commented:
\$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

Commented:
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

Commented:
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

Commented:
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 Commented:
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

Commented:
(\$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 Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.