Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Have a list of field names in an array.
fld1, fld2, fld3, fldn..

These correspond to Field values in a text string.
I am trying to compare two files to see what field has
changed and return those values.
eg. fld1: changed from v1 to v2.

ex. file
Event ID<~>Container Number<~>Batch Number<~>Batch Type
0100<~>IDRFtest<~>RTR0100<~>RTR
0101<~>IDRFtest<~>RTR0101<~>RTR
0102<~>IDRFtest<~>RTR0102<~>RTR

Not being a perl expert yet, here is what I have so far...
#!/usr/local/bin/perl -w print "Please input ldap file: "; chomp ($FILE_LDAP = <STDIN>); print "Please input sig file: "; chomp ($FILE_SIG = <STDIN>); $FIELD_LIST_CMP1 = `grep "Event ID" $FILE_LDAP`; $FIELD_LIST_CMP2 = `grep "Event ID" $FILE_SIG`; if ($FIELD_LIST_CMP1 ne $FIELD_LIST_CMP2) { print "List of fields do not match\!\n"; exit; } $FIELD_LIST = `grep "Event ID" $FILE_LDAP`; chomp $FIELD_LIST; push @FIELD_LIST, split ("<~>",$FIELD_LIST); @COMM = split /\n/,`comm -3 $FILE_LDAP $FILE_SIG`; &Check_EV; &Check_Text; sub Check_EV { # Check for <~> in the line. @FIELD_DIFF = grep /<~>/, @COMM; # This gets list of Event ID's for (@FIELD_DIFF) { # unix comm command seperates differences in columns if (!/^\s/) { @var = (split /<~>/, $_); push @EV_LIST, $var[0]; } } }

---------------------
sub Check_EV is where I need some help. Feel free to change anything..
I know there is a super hootie way of doing this. Please help.
Brandon Cole bcole23@hotmail.com

Replies are listed 'Best First'.
Re: Some tricky array/hash work
by jlongino (Parson) on Oct 10, 2001 at 07:27 UTC
    I suspect that there are a lack of replies to your post for several reasons:
    • Although you use -w you aren't using strict.
    • You're slinging all sorts of backticked xnix commands around without testing for errors.
    • What happens if the two files have more than one different line in them? What will happen if two files have no differences? These are some questions that are not addressed either by code, comments or your description of the problem.
    • Your're using xnix commands on file vars read from STDIN without testing to see they exist or not. Hopefully, you will be the only person running the program. An unscrupulous user could type nasty xnix commands instead of file names. You might want to consider taint checking.
    These are just a few problems. Sharper eyes than mine will certainly find more. You should probably consider these suggestions, rework your code, add more details defining your problem and post the revised version as an update. The more you do in preparing your post, the more likely that others will respond to it.

    "Make everything as simple as possible, but not simpler." -- Albert Einstein