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

Hello monks,
I have some problems with a string I got from
my $content = get $url;
where $url is a HTTP-source in the string $content.

In $content are lines with the structure

*.*$left_string $unknown_search_sting $right_string*.*\n
like:
"blablaaaa_SES_bbbblala" "reettaaa_SOS999_bbbtzz" "wertzaaa_T541_bbbaqyyyyyyy" "ffaaa_SES_bbbwwwwwwwww"
so
$left_string = "aaa_" $right_string = "_bbb"
I don't know, what $unknown_search_sting is at the beginning, but I have to count them like
SES 2 SOS999 1 T541 1
Thank a lot for Your help,
Roman

Replies are listed 'Best First'.
Re: How to use long strings...
by Abigail-II (Bishop) on Sep 03, 2003 at 08:51 UTC
    Assuming there's only one $left_string and only one $right_string per line, you might want to do something like:
    my %count; $count {$1} ++ while $content =~ /\Q$left_string\E(.*)\Q$right_str +ing\E/g;

    I haven't tested it though.

    Abigail

Re: How to use long strings...
by bobn (Chaplain) on Sep 03, 2003 at 08:42 UTC

    if your format is really:

    crud_GOOD_crud
    Then you can split it out:
    #untested for (split("\n", $content) ) # split string to multiple lines { my (undef, $good, undef) = split(/_/, $_); # could also be written as $good = ( split(/_/, $_) )[1]; $seen{$good}++; } for ( keys %seen ) { print "$_ $seen{$_}\n" }

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      The OP described the searched strings as rather "CRUD$left_string_GOOD_$right_stringCRUD" then just "crud_GOOD_crud". So you should split on /${left_string}_|_$right_string/.
      Thank You,
      for (split("\n", $content))
      {
      $good = ( split(/logo_/, $_) )1;
      $good1 = ( split (/_20x/, $good) ) [0];
      $seen{$good1}++;
      }

      for ( keys %seen )
      {
      print "$_ $seen{$_}
      ";
      }
      works.
      Can I sort the output by the numbers?
      In the output is an number, witch i can not find with
      grep logo_ count.html | cut -d\, -f4 | sort | countfirstString.awk | sort -t\, -k2nr | less
        May be, the unknown number are the number of lines, witch do not contain the "/logo" and/or "_20x"?
        How can I fix this?
        Thank You,
        Roman
Re: How to use long strings...
by BrowserUk (Patriarch) on Sep 03, 2003 at 09:00 UTC

    Question: If you string was

    somegarbage_TEST_SCRIPT_someotherstuff

    Do you want to count the occurances of 'TEST' or 'TEST_SCRIPT'?

    Is it possible that this could happen?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      I need :
      logo_idl_20x10.gif
      logo_pll_20x10.gif
      logo_cip_20x10.gif
      logo_pll_20x10.gif
      logo_cip_20x10.gif
      logo_pll_20x10.gif


      pll 3
      cip 2
      idl 1
      Thank You, Roman

        Assuming this is a complete list of the possibilities

        my $content = ....; my %counts; $counts{ $_ }++ for $content =~ m[logo_([^_]+)_20x10\.gif]g; print "$_ : $counts{ $_ }\n" for keys %counts;

        Though it make me wonder what all the obfuscation of $left_string $unknown_string $right_string and blaaahala etc. was all about?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: How to use long strings...
by Taulmarill (Deacon) on Sep 03, 2003 at 09:21 UTC
    yet another silly atemt to solve your problem:
    while (get $url) { my $string = (split "_")[1]; print "${string}\n" }
    untested but should work.

    --- edit ---
    ARRGGG! my fault!
    replace while with foreach and it schoud work. (Maybe you have to split "get $url" by "\n" so this would be better:
    forach (split "\n", (get $url)) {
Re: How to use long strings...
by Skeeve (Parson) on Sep 03, 2003 at 08:43 UTC
    Okay.
    So what is your question?
    How to count the number of occurences of strings?
    Hashes can do this. e.g.: ++$count{$string};
Now good I think ?!
by khelben (Initiate) on Sep 04, 2003 at 06:20 UTC
    my $content = get $url; my %counts; sub by_val { return -($counts{$a} <=> $counts{$b}); } $counts{ $_ }++ for $content =~ m[logo_([^_]+)_20x10\.gif]g; print "$_ $counts{$_}<br>" for sort by_val keys %counts;
    Thanx a lot for Your help!
    Roman
Re: How to use long strings...
by khelben (Initiate) on Sep 03, 2003 at 08:34 UTC
    Ups,
    i forgot to sign in :(