Solved

comparing "split" strings of an array with next element

Posted on 1998-11-09
13
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transpose into pipe delemited 8 86
crawling gofundme 4 151
quoting a comma separated list 20 93
Perl Versus AWK? 7 99
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

710 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