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: Re: [hwnd] How to join hash keys with data output?: Edit Log



Laurent_R
Veteran / Moderator

Jun 1, 2013, 1:25 PM


Views: 542
Re: [hwnd] How to join hash keys with data output?

Hi,

try this:


Code
use strict; 
use warnings;
use Data::Dumper;
my ($key_start, $key);
my %data_hash;
while (<DATA>) {
chomp;
$key_start = $1 if /^title: (\w+)/;
$key = $key_start . "-" . $1 if /^id: (\d+)/;
$data_hash{$key} = $1 if /^num: ([\s\d]+)/;
}
print Dumper %data_hash;



__DATA__
title: foo
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10
title: bar
id: 1
num: 1 2 3 4 5
id: 2
num: 6 7 8 9 10


This prints the following output:


Code
$ perl hash.pl 
$VAR1 = 'foo-1';
$VAR2 = '1 2 3 4 5 ';
$VAR3 = 'foo-2';
$VAR4 = '6 7 8 9 10 ';
$VAR5 = 'bar-1';
$VAR6 = '1 2 3 4 5 ';
$VAR7 = 'bar-2';
$VAR8 = '6 7 8 9 10';


UPDATE: I had not seen Chris had replied while I was typing my solution. Well, you've got two possible solutions now.


(This post was edited by Laurent_R on Jun 1, 2013, 1:28 PM)


Edit Log:
Post edited by Laurent_R (Veteran) on Jun 1, 2013, 1:28 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