bcoballe
asked on
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.
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.
#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
@ary=('abc:def:123:456:ghi
$res=(split(/:/,$ary[0]))[
print $res,"\n";
#may not be what you're looking for
Whoops : thoellri turned himself into a bozo ... what was I htinking then?
#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?
$res=join(':',(split(/:/,$
#but I'm more curious what is bcoballe thinking of?
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";
}
($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";
}
mazares:
What's the difference between
split(/:/,$array{$rec1})
and
split(/:/,$array[$rec1])
?
What's the difference between
split(/:/,$array{$rec1})
and
split(/:/,$array[$rec1])
?
$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
But that no bearing on what happens with the split
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 ...
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 :-)
@ary=("One", "Two", "Three");
print $ary["a"],"\n";
Now let's see how many people are completly confused :-)
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"
"a" == "0"
ASKER
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?
bcoballe
#perhaps you were thinking of
$res=join(':',(split(/:/,$
#but I'm more curious what is bcoballe thinking of?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
#perhaps you were thinking of
$key=join(':',(split(/:/,$
#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[
$key1 = join(':',(split(/:/,$ary[i
$key2 = join(':',(split(/:/,$ary[i
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
$res=(split(/:/,$ary[0]))[
print $res,"\n";
$res=(split(/:/,$ary[0]))[
print $res,"\n";
Is this what you're looking for?
Hope this helps
Tobias