Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression and file delimeters

Status
Not open for further replies.

jerehart

Programmer
Jun 10, 1999
61
US
Hey,

I have an input file that looks like:

$$Catagory1
^data one
^data two
... etc

$$Catagory2
^^data three
^^data four
... etc

$$Catagory2
^^^data six
^^^data five
... etc

I am looking for an easy way to parse this off the [^]+ and put them into seperate arrays any ideas?
 
Could you be a little more specific about what you want to do? What do you mean "separate arrays"? Does that mean category1 in one array and category 2 in another? Do you want to just remove all leading ^ and store the rest of each line? More input! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Not quite sure if this is what you what, but this is my take on it:

#!/usr/bin/perl

my $category = "";
my @values = ();
my %allentries;

open FILE, "data.txt" or die "cannot open data.txt\n";
while(<FILE>) {
chomp;
if(/^\$\$(.*)$/) {
$category = $1;
}elsif(/^\^+(.*)$/) {
push @{$allentries{$category}}, $1;
}
}
close FILE;

# for each category you can now access the values
# from the array along the lines of..

foreach my $cat (keys %allentries) {
foreach my $val (@{$allentries{$cat}}) {
print &quot;Value $val is in category $cat\n&quot;;
}
}

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top