perl_mystery has asked for the wisdom of the Perl Monks concerning the following question:
I need to construct a hash for a file whose input looks like below.A sample output is shown. 1.How do I construct a hash for the below input? 2.I have written a code to achieve this but I having trouble with the regex constructed for the key and value below,not sure what is wrong? 3.Printing the hash shows an empty output,why?
INPUT:-
.\root\edit\perl\scripts\scripths\sec\inc\script_auth_pap.h-113115;perforcePLF.txt;//programfiles/documents/data/lookup/script_auth_pap.h - label_scriptHS_source.01.16.00 : 5
.\root\edit\perl\scripts\scripths\sec\inc\script_auth_peap.h-34348;perforcePLF.txt;//programfiles/documents/data/lookup/script_auth_peap.h - label : 5
.\root\edit\perl\scripts\scripths\sec\inc\script_auth_peap.h-113116;perforcePLF.txt;//depot/old/text/data/script_auth_peap.h - label_scriptHS_source.01.16.00 : 5
.\root\edit\perl\scripts\scripths\sec\inc\script_auth_ttls.h-34349;perforcePLF.txt;//source/new/text/files/data/script_auth_ttls.h - label : 5
OUTPUT:-
HASH should like below
//programfiles/documents/data/lookup/script_auth_pap.h=>root\edit\perl\scripts\scripths\sec\inc\script_auth_pap.h
//programfiles/documents/data/lookup/script_auth_peap.h=>root\edit\perl\scripts\scripths\sec\inc\script_auth_peap.h
//depot/old/text/data/script_auth_peap.h=>\root\edit\perl\scripts\scripths\sec\inc\script_auth_peap.h
//source/new/text/files/data/script_auth_ttls.h=>root\edit\perl\scripts\scripths\sec\inc\script_auth_ttls.h
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; open my $fh, '<', $ARGV[0] or die "could not open $ARGV[0]'' $!"; while (my $line = <$fh>) { my $key= $line =~/;(.*)\s-\s/; #match anything between ; and - is k +ey my $value= $line =~/\.\\(.*)-\d+\;/; #match anything between .\ and - + is value $hash{$key}=$value; } print Dumper(\%hash);
|
|---|