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



2teez
Novice

Jun 1, 2013, 2:20 PM


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

Have you tried the solution given by Zhris? I think it gives a type of output you want. However, if you don't mind you can toy with this also. Just the same concept.

Code
#!/usr/bin/perl 
use warnings;
use strict;

my %hash_data;
my $key;

while (<DATA>) {
s/\s+$//;
if (/title:\s+?(.+)/) {
$key = $1;
}
elsif (/id:\s+?(.+)/) {
$key .= '-' . $1;
}
elsif (/num:/) {
$hash_data{$key} = ( split /\w+:/, $_ )[1];
$key =~ s/-.+$//;
}

}
use Data::Dumper;
print Dumper \%hash_data;
__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]

Output:

Code
$VAR1 = { 
'foo-1' => ' 1 2 3 4 5',
'foo-2' => ' 6 7 8 9 10',
'bar-1' => ' 1 2 3 4 5',
'bar-2' => ' 6 7 8 9 10'
};



(This post was edited by 2teez on Jun 1, 2013, 2:25 PM)


Edit Log:
Post edited by 2teez (Novice) on Jun 1, 2013, 2:25 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