Title: RE: Pattern Matching

Ruebel,

If I understand your intent correctly, it's because the scope of your "if" statement is too large.  You need to collect ALL lines BEFORE enumerating and printing them out.

I haven't run this code, but I think this is what you mean to say:

# - initialize
undef @items;
undef %qty;

# - collect items
open (FILE,"yourfile.txt");
while ($line=<FILE>) {
  if ($line =~ /^Processor.+?x86 Family (\d+) Model (\d+) Stepping (\d+) GenuineIntel ~?(\d+) Mhz$/i) {
   my $Fam  = $1;
   my $Mod  = $2;
   my $Step = $3;
   my $Speed = $4;
   push(@items, "($Fam/$Mod/$Step) $Speed Mhz");
}
close(FILE);

# - enumerate
foreach $item (@items) {
   $qty{$item}++;
}
# - print them out
foreach $item (sort keys %qty){
   $quantity = $qty{$item};
   $quantity = sprintf("%01d",$quantity);
   print "$quantity $item\n";
}

-----Original Message-----
From: Ruebel Oliver [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 21, 2001 10:05 AM
To: '[EMAIL PROTECTED]'
Subject: AW: Pattern Matching


This is my if-sentences inside my perl-program now:

if($line =~ /^Processor.+?x86 Family (\d+) Model (\d+) Stepping (\d+)
GenuineIntel ~?(\d+) Mhz$/i) {
                my $Fam  = $1;
                my $Mod  = $2;
                my $Step = $3;
                my $Speed = $4;
                @items = "($Fam/$Mod/$Step) $Speed Mhz";
                foreach $item (@items) {
                        $qty{$item}++;
                        }
                foreach $item (sort keys %qty){
                        $quantity = $qty{$item};
                        $quantity = sprintf("%01d",$quantity);
                        print "$quantity $item\n";
               }
        }

But I still get two lines of result:

1 (6/8/3) 694 Mhz
2 (6/8/3) 694 Mhz

and I only need the second line as my result, so it should look like:

2 (6/8/3) 694 Mhz

Do you have any suggestion?
Thanks a lot!!!


> -----Urspr�ngliche Nachricht-----
> Von:  [EMAIL PROTECTED] [SMTP:[EMAIL PROTECTED]]
> Gesendet am:  Mittwoch, 21. M�rz 2001 15:35 Uhr  Ruebel
> An:   [EMAIL PROTECTED]
> Betreff:      RE: Pattern Matching
>
> #!/usr/bin/perl
>
> # - set up the list
> @items = (
>         "1Mb network card",
>         "10Mb network card",
>         "100Mb network card",
>         "1Mb network card",
>         "Laptop",
>         "Monitor",
>         "Laptop",
> );
>
> # - here's the magic
> foreach $item (@items) {
>         $qty{$item}++;
> }
>
> # - print them out
> foreach $item (sort keys %qty) {
>         $quantity=$qty{$item};
>         $quantity=sprintf("%02d",$quantity);
>         print "($quantity) $item\n";
> }
>
> -----Original Message-----
> From: Ruebel Oliver [ <mailto:[EMAIL PROTECTED]>]
> Sent: Wednesday, March 21, 2001 3:13 AM
> To: [EMAIL PROTECTED]
> Subject: Pattern Matching
>
>
> How can I get through pattern matching an operation that, if there a two
> similar lines (I�m searching for) the program counts them and gives me
> only one line output with the number of hits in front.
>
> Like when the program founds 2 lines with:
>
> Network Interface Card 10 Mbit
> Network Interface Card 10 Mbit
>
> It gives me the output:
>
> 2 Network Interface Card 10 Mbit
>
> Thanks in advance!
>

Reply via email to