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

 



matchless
Novice

Jan 10, 2013, 7:16 PM

Post #1 of 9 (1295 views)
odd name/value split, or is it a split? Can't Post

I need to read the name/value pairs from *a single selected line* from an unsorted flatfile whose fields names may vary and some may be empty. The line is selected by the ID field. The unique field value is always the ID field. Many other fields are the same, with a fiew oddball name/value pairs not shared in other lines.

The fields go by $item,$value pairs split with a colon, with a pipe separator in between. Example:


Code
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|
colors:|dog:|city:San Diego|birth:|marriedyr:1988|spouse:Tom|id:789|


Normally I could do this with a WHILE and split name/value pairs by the pipe character, but I'm stumped as how to do this. The data fields need to populate a form, FYI.Unsure


rovf
Veteran

Jan 10, 2013, 10:49 PM

Post #2 of 9 (1279 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post


Quote
split name/value pairs by the pipe character, but I'm stumped as how to do this.


See perldoc -f split. Note that you don't specify the string, where to split, but a regexp. Since the pipe character has a special meaning in a regexp, you need to escape it, i.e.


Code
my @fields=split(/\|/,$your_line_goes_here);



matchless
Novice

Jan 10, 2013, 10:57 PM

Post #3 of 9 (1278 views)
Re: [rovf] 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.


Laurent_R
Veteran / Moderator

Jan 10, 2013, 11:53 PM

Post #4 of 9 (1273 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post

I have trouble understanding exactly what you want, but it seems to me that you can just do ther split in all cases, and decide only afterwards whether tyou neet to do something with the line.


rovf
Veteran

Jan 10, 2013, 11:53 PM

Post #5 of 9 (1273 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post

You can split every line on the '|'. This gives you a list of strings of the form NAME:VALUE. You now turn this list into a hash. Then you can access the 'id' field of this hash. If this is the id you are looking for, you've found your line.

As an alternative to building the hash, you can also loop through the list until you find a string which has the form 'id:NNNN'.


matchless
Novice

Jan 11, 2013, 3:18 AM

Post #6 of 9 (1269 views)
Re: [rovf] odd name/value split, or is it a split? [In reply to] Can't Post


Code
foreach $line (@data) { 
@fields = split(/\|/, $line);
foreach $pair (@fields) {
@target = split(/\:/, $pair);
...


Something like this?


rovf
Veteran

Jan 11, 2013, 5:31 AM

Post #7 of 9 (1267 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post

Looks reasonable. Of course you should localise the variables with my.


Chris Charley
User

Jan 11, 2013, 12:47 PM

Post #8 of 9 (1247 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post

Here is a possible solution. Don't know what you want for output so you would have to determine that. I just dummied up some output at the end of the program.


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

my %data;
my $file = 'junk.txt';

open my $fh, "<", $file or die "Unable to open $file for reading. $!";
while (<$fh>) {
chomp;
my ($id) = /id:(\d+)/ or die "Line $. is missing ID - $file\n";
$data{$id} = { map $_ || 'NA', split /[|:]/ }; # 'NA' == not available
}
close $fh or die "Unable to close $file. $!";

while (my ($id, $href) = each %data) {
print "id:$id married:$href->{marriedyr} spouse:$href->{spouse}\n";
}

__END__
*** junk.txt ***
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|
colors:|dog:|city:San Diego|birth:|marriedyr:1988|spouse:Tom|id:789|


The output was:

Code
 
C:\Old_Data\perlp>perl t33.pl
id:304 married:NA spouse:Tom
id:5466 married:1999 spouse:Bob
id:789 married:1988 spouse:Tom



7stud
Enthusiast

Jan 11, 2013, 9:34 PM

Post #9 of 9 (1229 views)
Re: [matchless] odd name/value split, or is it a split? [In reply to] Can't Post


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)

 
 


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

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