CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Accessing each line of file with nested delimiters

 



Tejas
User

Sep 20, 2016, 2:56 PM

Post #1 of 3 (2423 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


Code
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


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


FishMonger
Veteran / Moderator

Sep 20, 2016, 3:06 PM

Post #2 of 3 (2418 views)
Re: [Tejas] Accessing each line of file with tested delimiters [In reply to] Can't Post

Using your 2 sample lines of input, show us the actual hash structure you expect.

Why aren't you doing the additional splitting and building of the hash outside of the sub? I'd split out the $values var inside the sub and build the full data structure in that sub.


Tejas
User

Sep 20, 2016, 3:13 PM

Post #3 of 3 (2414 views)
Re: [FishMonger] Accessing each line of file with tested delimiters [In reply to] Can't Post

True, i thought it would confuse the readers, so just divided the stuff.
I will have everything in the same sub finally

Also, the stuff that is outside the sub is just expected result and the idea i have on my mind to achieve the result and not the real code.

Thanks


(This post was edited by Tejas on Sep 20, 2016, 3:15 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives