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: [matchless] odd name/value split, or is it a split? : Edit Log



7stud
Enthusiast

Jan 11, 2013, 9:34 PM


Views: 967
Re: [matchless] odd name/value split, or is it a split?


In Reply To
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.



Code
 
my $str = "city:Oakland|marriedyr:1999|id:5466|colors:peach|birth:1976|death:|spouse:Bob|";

if ($str =~ /id:(\d+)/) {
say $1;
}


--output:--
5466


Code
 
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|


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


Edit Log:
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 9:57 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:12 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:13 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:18 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:25 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:26 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:28 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:58 PM
Post edited by 7stud (Enthusiast) on Jan 11, 2013, 10:59 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