de2425 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks;

I am hoping that you might be able to help me with something with which I am having trouble even figuring out where to start. I have a file that has a column in it of the following form:

Category1

Category1--Subcategory1--subsubcategory1--etc.

Category1--Subcategory1--subsubcategory2--etc.

Category1--Subcategory2--subsubcategory1--etc.

Category1--Subcategory2--subsubcategory2--etc.

Category2

Category2--Subcategory1--subsubcategory1--etc.

Category2--Subcategory2--subsubcategory1--etc.

What I would like to do is create an index that would number each category and subcategory within it. So according to the info above, it would look something like this:

1

1.1.1

1.1.2

1.2.1

1.2.2

2

2.1.1

2.2.1

I don't have much yet in the way of code because I'm not sure which way would be best to incriment each time there was a change. I was thinking separating hashes based on the main category but I still am unsure how that would work out. Does anyone have any suggestions? Any help would be greatly appreciated.

Here's How I've started my code anyway:

#!/usr/bin/perl -w use strict; open (IN, "C:/Work/start_file.txt"); open (OUT, ">C:/Work/finish_file.txt"); while (<IN>){ chomp; my @t=split(/\t/); my @categories=split(/--/,$t[0]); } close OUT; close IN;

Replies are listed 'Best First'.
Re: Do Something Until a Variable's Value Changes
by ikegami (Patriarch) on Apr 24, 2009 at 15:00 UTC

    The pattern you need is

    my $last; while (defined($_ = next())) { if (!defined($last) || different($_, $last)) { new_group(); $last = $_; } else { same_group(); } }

    Replace next, different, new_group and same_group with something appropriate to your problem. Some problems require more than one $last.

Re: Do Something Until a Variable's Value Changes
by wfsp (Abbot) on Apr 24, 2009 at 15:00 UTC
    Something like this might get you started?
    #!/usr/bin/perl use warnings; use strict; while (my $line = <DATA>){ next unless $line =~ /\S/; chomp $line; my @cats = $line =~ /(\d+)/g; my $rec = join(q{.}, @cats); print qq{$rec\n}; } __DATA__ Category1 Category1--Subcategory1--subsubcategory1--etc. Category1--Subcategory1--subsubcategory2--etc. Category1--Subcategory2--subsubcategory1--etc. Category1--Subcategory2--subsubcategory2--etc. Category2 Category2--Subcategory1--subsubcategory1--etc. Category2--Subcategory2--subsubcategory1--etc.
    1 1.1.1 1.1.2 1.2.1 1.2.2 2 2.1.1 2.2.1
Re: Do Something Until a Variable's Value Changes
by targetsmart (Curate) on Apr 24, 2009 at 14:57 UTC
    Hashes of hashes will be okay for this.
    $hash{maincategory}{subcategory}{subsubcategory} = some value;
    see perldsc

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Do Something Until a Variable's Value Changes
by spx2 (Deacon) on Apr 24, 2009 at 15:00 UTC
    You haven't shown any samples for "start_file.txt" or "finish_file.txt". What file format are they written in ?