Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Comparing a list to a tab delimited text file

by Laurent_R (Canon)
on Mar 15, 2018 at 17:03 UTC ( [id://1210972]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Comparing a list to a tab delimited text file
in thread Comparing a list to a tab delimited text file

OK, if I understand well, what you need to do is to store the content of your tab separated file into a hash where the key would be the first column of each line of this file and the value the rest of the line (or the full line, or an array of the fields, whatever suits best your needs). Your tab separated file is relatively small and should fit into memory.

After having done that, you read your XML file and, for each node of interest, you look up the hash to see if the word exists. If it does, you retrieve the information from the corresponding value and update your XML content.

Pseudo-code for building the hash:

my %hash; open my $TAB, "<", $tab_file or die "Cannot open $tabfile $!"; while (my $line = <$TAB>) { my $key = (split /\t/, $line)[0]; $hash[$key] = $line; } close $TAB;
Then, when reading the XML file, you lookup the word of interest in the hash:
if (exists $hash{$xml_word}) { my $line = $hash{$xml_word}; # now do what you want with $line }
You could also preprocess the $line when reading the tab separated file and store in the hash not the full line, but only the part of the data that you need, in a format that makes it easier to use (perhaps as an array ref, for example). Whether this is a good idea depends on your data (are you likely to user almost all entries of the TSV file, or only a few of them? Are you likely to use the TSV entries several times? and so on).

Replies are listed 'Best First'.
Re^4: Comparing a list to a tab delimited text file
by Azaghal (Novice) on Mar 19, 2018 at 10:22 UTC

    Thanks a lot for this answer, it works perfectly and my script now runs fast enough, no need to preprocess $line.

    For future reference there's just one typo in your pseudo code, $hash[$key] should be $hash{$key}.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found