×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

setting variables after data file record match?

setting variables after data file record match?

setting variables after data file record match?

(OP)
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, "<$file") or &error("Unable to open the data file");
@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,

RE: setting variables after data file record match?

Try this:

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

Let me know if there are any problems with this...

RE: setting variables after data file record match?

(OP)
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, "<$file") or &error("Unable to open the data file");
@data=<data>;
close(data);
while (<data>) {
     chomp();
     if (^/Mark/) {
          ($check1, $check2, $check3, $check4) = split(/:/);
     }
}

Any ideas?

RE: setting variables after data file record match?

you need to remove @data=<data> and replace it with the code I added..

RE: setting variables after data file record match?

(OP)
Ok, sorry for this Perl is still new to me. But it still gives an Internal Error with your code in between:

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

RE: setting variables after data file record match?

hi there,

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

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

The ^ does something called "anchoring".

When you say something like /$check1/ you're saying "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.

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: setting variables after data file record match?

(OP)
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!

RE: setting variables after data file record match?

(OP)
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?

RE: setting variables after data file record match?

(OP)
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

RE: setting variables after data file record match?

(OP)
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?

RE: setting variables after data file record match?

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



RE: setting variables after data file record match?

(OP)
Ok, that was a stupid question, thanks for being patient!

RE: setting variables after data file record match?

(OP)
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

RE: setting variables after data file record match?

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

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

# and then, to work through the array
foreach $array_elemnt (@match_array){
# and your code goes here...
}

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: setting variables after data file record match?

(OP)
Thanks Mike, I will try this,

raoul

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close