Hi Mike,
Try something like the following. It actually takes an extra step, by making a hash
opf all lines, then searching it for the keys that have the desired numerical prefix.
There is a purpose to that. A few small edits will allow this to report all of the
strings, grouped by prefix.
#!/usr/bin/perl -w
my ($SourceFile, $SoughtPrefix) = @ARGV;
open (SOURCE, $SourceFile);
my $i = 0;
while (<SOURCE>) {
chomp;
if(!$_ or $_ eq "") {next;}
($value, $name) = split(/ +/, $_, 2); # Didn't have the 2 at first--"blue whale"
became "blue"
$contents{$name} = $value;
$groups{$value} = $value;
}
if ($SoughtPrefix and $SoughtPrefix ne "") {
SeekPrefix();
} else {
foreach $group(sort keys(%groups)) {
$SoughtPrefix = $group;
print "PREFIX: $SoughtPrefix\n";
SeekPrefix();
print "**************************\n";
}
}
sub SeekPrefix {
my $i = 0;
my @FoundStrings;
foreach $key (keys(%contents)) {
if ($contents{$key} =~ /$SoughtPrefix/) {
$FoundStrings[$i++] = $key;
print "$key\n";
}
}
my $MatchCount = $i;
print "Ther were $MatchCount strings that matched\n";
}
Output:
E:\d_drive\perlStuff\guests>GroupTest.pl values.txt 012
"blue whale"
"lion"
Ther were 2 strings that matched
E:\d_drive\perlStuff\guests>GroupTest.pl values.txt
PREFIX: 010
"blue"
"red"
Ther were 2 strings that matched
**************************
PREFIX: 011
"diamond"
"emerald"
Ther were 2 strings that matched
**************************
PREFIX: 012
"blue whale"
"lion"
Ther were 2 strings that matched
**************************
Mike Burnard wrote:
> Hi,
>
> I'm in the midst of writing my first useful perl script. It's designed to
> pull text out of a plain text file and write a xml plist.
>
> The only trouble I'm having (so far) is getting the text out the way I want
> it.
>
> The file I'm pulling data out of is formatted like this:
>
> 010 "red"
>
> 011 "diamond"
>
> 012 "lion"
>
> 010 "blue"
>
> 012 "blue whale"
>
> 011 "emerald"
>
> So, I've got this to get specific groups out at once:
>
> while (<OLDHL>) {
> if(/^010/) {
> -- part I need help with --
> }
> }
>
> I just need to know what to put to get only the part within quotes out of
> each line. I'm going to put them into either an array, but I can figure
> that part out, its just getting only part of the line I'm having trouble
> with.
>
> I think I'll have no problem writing the xml file with printf, other than
> this small problem I'm having fun. I was proud of myself when I got all of
> the 010's to print to the screen at least!.
>
> Thanks for your help.
>
> -mike
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]