
7stud
Enthusiast
Jan 11, 2013, 9:34 PM
Post #9 of 9
(504 views)
|
|
Re: [matchless] odd name/value split, or is it a split?
[In reply to]
|
Can't Post
|
|
I'm having a problem selecting the line to read by ID. I need to isolate that line before getting the $item, $value pairs, but I can't select the line to view the value of $item(id) for a match to my query without some kind of regex or split.
my $str = "city:Oakland|marriedyr:1999|id:5466|colors:peach|birth:1976|death:|spouse:Bob|"; if ($str =~ /id:(\d+)/) { say $1; } --output:-- 5466 use strict; use warnings; use 5.012; my %results_for; while (my $line = <DATA>) { if ($line =~ /id:(\d+)/) { my $id = $1; chomp $line; my %data = split /[|:]/, $line; delete $data{'id'}; $results_for{$id} = \%data; } } use Data::Dumper; say Dumper(\%results_for); my $target_id = 5466; say Dumper($results_for{$target_id}); __END__ no_id:move_on city:Oakland|marriedyr:1999|id:5466|colors:peach|birth:1976|death:|spouse:Bob| marriedyr:|id:304|colors:|city:San Diego|birth:|spouse:Tom|dog:Spot| no_id:move_on colors:|dog:|city:San Diego|birth:|marriedyr:1988|spouse:Tom|id:789| --output:-- $VAR1 = { '304' => { 'spouse' => 'Tom', 'birth' => '', 'city' => 'San Diego', 'marriedyr' => '', 'dog' => 'Spot', 'colors' => '' }, '5466' => { 'spouse' => 'Bob', 'birth' => '1976', 'marriedyr' => '1999', 'city' => 'Oakland', 'death' => '', 'colors' => 'peach' }, '789' => { 'spouse' => 'Tom', 'birth' => '', 'marriedyr' => '1988', 'city' => 'San Diego', 'dog' => '', 'colors' => '' } }; $VAR1 = { 'spouse' => 'Bob', 'birth' => '1976', 'marriedyr' => '1999', 'city' => 'Oakland', 'death' => '', 'colors' => 'peach' };
(This post was edited by 7stud on Jan 11, 2013, 10:59 PM)
|