
Tejas
User
Sep 20, 2016, 2:56 PM
Post #1 of 3
(2754 views)
|
Accessing each line of file with nested delimiters
|
Can't Post
|
|
I have managed implementing the below code and stuck at a point where i felt i am going in a wrong path
use strict; use warnings; my %result_hash = (); my %final_hash = (); Compareresults(); foreach my $key (sort keys %result_hash ){ print "$key \n"; print "$result_hash{$key} \n"; #split the value of result_hash{$key} again by , and see whether any chunk is seperated by ; #example : 7802315095\d\d,7802315098\d\d;7802025001\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d #every chunk without ; and value on left with ; should be stored here @{$final_hash{"eto"} }= ['7802315095\d\d','7802315098\d\d','7802025002\d\d','7802025003\d\d','7802025004\d\d','7802025005\d\d','7802025006\d\d','7802025007\d\d'] ; @{ $final_hash{"pro"} } = ['7802025001\d\d'] ; #Anything found on the right side of ; has to be stored in this hash } sub Compareresults { while ( <DATA> ) { my($instance,$values) = split /\:/, $_; $result_hash{$instance} = $values; } } __DATA__ 1:7802315095\d\d,7802315098\d\d;7802025001\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d 2:7802315095\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d In the data above aim trying to read the file 1. get the chunk separate by : 2. store it in hash with the number on the left of : as key 3.get values of each key and store it again in a hash 4.if any value has ; in it then the value at the left is store Din another hash Am i making it complicated
Output Expected , It has to result the values and check for ; and store it in corresponding key i.e anything on left of ; goes to @{$final_hash{"eto"} } and right side goes to @{$final_hash{"pro"} } @{$final_hash{"eto"} }= ['7802315095\d\d','7802315098\d\d','7802025002\d\d','7802025003\d\d','7802025004\d\d','7802025005\d\d','7802025006\d\d','7802025007\d\d'] ; @{ $final_hash{"pro"} } = ['7802025001\d\d'] ; Thanks Tejas
(This post was edited by Tejas on Sep 20, 2016, 3:11 PM)
|