I don't think that the string functions are the way to go here. I would use regex to make 2 hashes and then merge/adjust them in whatever way that you need. This is some example code:
#!/usr/bin/perl -w use strict; use Data::Dumper; open (my $config_file_main ,"<","config18.txt" )or die $!; open (my $config_file_update ,"<","config27.txt" )or die $!; my %file1 = read_cfg ($config_file_main); my %file2 = read_cfg ($config_file_update); # this combines both hashes into one !!!! # A simple "merge" operation. # More complex things can be done %file1 = (%file1, %file2); # Usually it is better to print in # a human readable format with sort # foreach my $key (sort keys %file1) { print "$key = $file1{$key}\n"; } # This returns a list instead of list ref # Of course using a ref is more efficient, but here I # do something simple to illustrate points above.. # sub read_cfg { my $file_handle = shift; my %hash; while (<$file_handle>) { next if /^\s*$/; #skip blank lines my ($name,$value) = $_ =~ /^\s*(\S+)\s*=>\s*(\S+)/; $hash{$name} = $value; } close ($file_handle); return %hash; }
config27: CONFIG_BRIDGE_NF_EBTABLES => m CONFIG_BRIDGE_EBT_BROUTE => y CONFIG_BRIDGE_EBT_T_FILTER => z
config18: CONFIG_BRIDGE_NF_EBTABLES => m CONFIG_BRIDGE_EBT_BROUTE => m CONFIG_BRIDGE_EBT_T_FILTER => m CONFIG_BRIDGE_EBT_T_NAT => m CONFIG_BRIDGE_EBT_802_3 => m CONFIG_BRIDGE_EBT_AMONG => m CONFIG_BRIDGE_EBT_ARP => m CONFIG_BRIDGE_EBT_IP => m CONFIG_BRIDGE_EBT_LIMIT => m CONFIG_BRIDGE_EBT_MARK => m CONFIG_BRIDGE_EBT_PKTTYPE => m CONFIG_BRIDGE_EBT_STP => m CONFIG_BRIDGE_EBT_VLAN => m CONFIG_BRIDGE_EBT_ARPREPLY => m CONFIG_BRIDGE_EBT_DNAT => m CONFIG_BRIDGE_EBT_MARK_T => m CONFIG_BRIDGE_EBT_REDIRECT => m CONFIG_BRIDGE_EBT_SNAT => m CONFIG_BRIDGE_EBT_LOG => m CONFIG_BRIDGE_EBT_ULOG => m
Prints:
CONFIG_BRIDGE_EBT_802_3 = m CONFIG_BRIDGE_EBT_AMONG = m CONFIG_BRIDGE_EBT_ARP = m CONFIG_BRIDGE_EBT_ARPREPLY = m CONFIG_BRIDGE_EBT_BROUTE = y CONFIG_BRIDGE_EBT_DNAT = m CONFIG_BRIDGE_EBT_IP = m CONFIG_BRIDGE_EBT_LIMIT = m CONFIG_BRIDGE_EBT_LOG = m CONFIG_BRIDGE_EBT_MARK = m CONFIG_BRIDGE_EBT_MARK_T = m CONFIG_BRIDGE_EBT_PKTTYPE = m CONFIG_BRIDGE_EBT_REDIRECT = m CONFIG_BRIDGE_EBT_SNAT = m CONFIG_BRIDGE_EBT_STP = m CONFIG_BRIDGE_EBT_T_FILTER = z CONFIG_BRIDGE_EBT_T_NAT = m CONFIG_BRIDGE_EBT_ULOG = m CONFIG_BRIDGE_EBT_VLAN = m CONFIG_BRIDGE_NF_EBTABLES = m

In reply to Re: Hash , Keys strangely chopped !! by Marshall
in thread Hash , Keys strangely chopped !! by onkar

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.