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);

In reply to Constructing a hash - why isn't my regex matching anything by perl_mystery

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.