Solved

comparing "split" strings of an array with next element

Posted on 1998-11-09
13
259 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
grep that displays 4 lines above & 1 line below of what's found 10 74
Perl Awk Need Help 3 94
Awk Question 2 116
Perl tar error 8 31
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

21 Experts available now in Live!

Get 1:1 Help Now