On Thu, 28 Jun 2001, Seitz, Scott wrote:
> I'm having trouble with what I think is a very simple split question.
>
> I've got a line of text something like:
>
> DimView 1 "All" DimView 2 "Some" DimView 3 "Most" DimView 4 "None"
>
> I want a hash with (1, "All", 2, "Some", 3, "Most", 4, "None")
>
> I'm spitting on /DimView/, but I can't get the syntax to throw it into the
> hash correct. Can one of you thow me a couple of lines of code to get me
> through this?
I was able to get this to work:
#!/usr/bin/perl -w
use strict ;
my $data = 'DimView 1 "All" DimView 2 "Some" DimView 3 "Most" DimView 4 "None"';
my @fields = split /\s*DimView\s+/, $data;
my %field_data;
foreach (@fields) {
my($key, $value) = split / /;
$field_data{$key} = $value if $key;
}
foreach my $i (keys %field_data) {
print "$i = $field_data{$i}\n";
}
Output:
~$ perl testout.pl
1 = "All"
2 = "Some"
3 = "Most"
4 = "None"
~$
Now everyone can tell us how it can be done even better. :-)
-- Brett
http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
San Francisco isn't what it used to be, and it never was.
-- Herb Caen