Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting variables after data file record match?

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
I have a data file than contains 4 variables seperated by “:” for each record, like

Mark:25:male:sports

To open the file I use:

open (data, &quot;<$file&quot;) or &error(&quot;Unable to open the data file&quot;);
@data=<data>;
close(data);

What I would like to do is check the first variable of each record against a variable $check1. If they match I would like to set the following three variables of this record to the corresponding values. The result of this for the example above would be:

If $check1= “Mark”

$check2 = “24”
$check3 = “male”
$check4 = “sports”

Could some body show me what I should add to the code above to accomplish this?

Thanks,
[sig][/sig]
 
Try this:

while (<data>) {
chomp();
if (^/Mark/) {
($check1, $check2, $check3, $check4) = split(/:/);
}
}

Let me know if there are any problems with this... [sig][/sig]
 
Thanks damann, but it resulted in Internal Errors. (opening and closing the file is ok).
I also tried to replace Mark for $name but didn't help.

This is the script:

open (data, &quot;<$file&quot;) or &error(&quot;Unable to open the data file&quot;);
@data=<data>;
close(data);
while (<data>) {
chomp();
if (^/Mark/) {
($check1, $check2, $check3, $check4) = split(/:/);
}
}

Any ideas? [sig][/sig]
 
you need to remove @data=<data> and replace it with the code I added.. [sig][/sig]
 
Ok, sorry for this Perl is still new to me. But it still gives an Internal Error with your code in between:

open (data, &quot;<$passfile&quot;) or &error(&quot;Unable to open the data file&quot;);
while (<data>) {
chomp();
if (^/$Mark/) {
($check1, $check2, $check3, $check4) = split(/:/);
}
}
close(data); [sig][/sig]
 
hi there,

you need to put the ^ character *inside* the match thingies, like this:

$check1= 'Mark';
open (data, &quot;<$passfile&quot;) or &error(&quot;Unable to open the data file&quot;);
while (<data>) {
chomp();
if (/^$check1/) {
($check1, $check2, $check3, $check4) = split(/:/);
}
}
close(data);

The ^ does something called &quot;anchoring&quot;.

When you say something like /$check1/ you're saying &quot;look for the string in $check1 in the Perl variable $_ (which automatically gets filled in this kind of while loop)

When you say /^$check1/ the ^ anchors the search of $_ to the *beginning* of $_ -- so it will match this string:

'Mark Latham'

but not:

'Latham, Mark'

or anything else not beginning with the contents of $check1. [sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
Yes, that worked and thanks for explaining things aswell.

So if I would skip ^. And I would search for 'Latham', it would find 'Mark Latham'. Will it also find 'lathamandmore'?

Well I'm going to experiment a bit with this although it is not really necessary for this script but nice to know and to learn from! [sig][/sig]
 
rtb

sort of -- if you skip the ^ it *will* find 'Mark Latham' and 'Lathamandmore -- but not 'lathamandmore', it's a case sensitive thing.

oh and no problem, a pleasure.
[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
Actually it is not doing what I would like to,

The values of the four variables are always the values of the last record so instead of setting the other variables only when $check1 matches the first field it is setting the values in each record and shows those of the last! My quess? [sig][/sig]
 
add this, so that you can see what it's doing

if (/^$check1/) {
[tab]print;
[tab]($check1, $check2, $check3, $check4) = split(/:/);
}
[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
I changed the way of setting $check1 and now it seems to work correctly, although I can't see why it choose to set the four variables for each record initially.

Thanks anyway for the tip,

Raoul

[sig][/sig]
 
I run into a new problem. Matching a variable with the first field works now but how do you match a variable with the third field of a record? [sig][/sig]
 
hi rtb1,

one can construct /^(\w*):(\w*):(\w*):(\w*)$/ would give u $1=Mark $2=25 $3=male $4=sports

ofcourse nothing stops u from ($check1, $check2, $check3, $check4) = split(/:/); first

and then match each check (1|2|3|4) == _whatever_var

hope this helps..

-shail



[sig][/sig]
 
($check1, $check2, $check3, $check4) = split(/:/);
if($check3 =~ /matchstring/){
.
.
.
}
[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
Ok, that was a stupid question, thanks for being patient! [sig][/sig]
 
As you might have gathered I am a newbie in this field who’s experimenting a bit.

The last action (the match for field 3) might result in a number of matches.

How do I create an array of all the first fields of each record that match this third field and how do I access each item of the array if I would like to print that particular item or something like that?

What I would like to achieve is check the data file for matches in field 3 and create a form with a combobox with all the names ($check1) inside it that match on field 3.

raoul
[sig][/sig]
 
Hi Raoul,

Can't help you with the HTML stuff, as I know as much about that as a pig does about Sunday, but try this:

my @match_array;
my $array_elemnt;

while... { # as you work through the file

[tab]($check1, $check2, $check3, $check4) = split(/:/);
[tab]push @match_array, $_ if $check3 =~ /matchstring/;
}

# and then, to work through the array
foreach $array_elemnt (@match_array){
# and your code goes here...
}
[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br> [/sig]
 
Thanks Mike, I will try this,

raoul [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top