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

Hello Fellow Monks

I have a problem that I beleive should be quite simple but I just don't know the syntax for what I want to do.

I have a file containing lots of documents separated by checkpoints identified by the special sequence af characters at the start of the line. Within the checpoint is a unique identifier.

In another file, I have a list of unique identifiers that I want to use to modify the checkpoint.

If I slurp the list of IDs into an array, then read the documents in one line at a time, how can I tell if any of the IDs in the ID list are contained in the checkpoint

Thanks in advance

Moriarty

  • Comment on Finding a variable string within a variable string.

Replies are listed 'Best First'.
Re: Finding a variable string within a variable string.
by BrowserUk (Patriarch) on Dec 14, 2004 at 07:11 UTC

    A few lines (5 or 10) of sample data indicating what you want to look for, and where it is found in one of the examples would greatly clarify this question.


    Examine what is said, not who speaks.        The end of an era!
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Finding a variable string within a variable string.
by sasikumar (Monk) on Dec 14, 2004 at 09:46 UTC
    Hi
    Its realy hard to find your requirement please post some
    examples to make the monks clear about what you need.

    Just look at the grep function in the perl documentation and
    the s/// patern substitution. Both of this can serve your needs.

    Thanks
    Sasi Kumar
Re: Finding a variable string within a variable string.
by ysth (Canon) on Dec 14, 2004 at 10:24 UTC
    Use a hash instead? Sample data (in and out) would be helpful in determining exactly what you are asking here.
Re: Finding a variable string within a variable string.
by Moriarty (Abbot) on Dec 14, 2004 at 21:04 UTC

    It appears that my question wasn't as simple as I thought it might be. That maybe because I didn't express myself clearly so I'll have another go

    I haven't provided samples because

    • I didn't want to risk providing anything that may be considered proprietry
    • I didn't beleive it was necessary
    • I was after a generic answer (I would like to use this on different jobs and in each job the checkpoint is constructed differently)

    I know that m/string/ will tell me if "string" is in $_, and I assume m/$string/ will tell me if the string stored in $string is in $_, but what I need to know is the syntax for finding out if $string1 contains $string2 anywhere (something like C's strstr function). I don't need to know where in $string1 $string2 was found, nor do I want $string1 changed unless I find $string2 within it, I only need to know if it was found.

    If I still haven't explained myself properly, or more information is needed, I will try to come up with examples that don't contain propriety information.

    Moriarty

      You can either use regexps, or index
      if ($string1 =~ /\Q$string2\E/){...} if (index($string1, $string2) != -1){...}
      The \Q and \E are to quote metacharacters (so if $string2 is "f.o", you don't end up matching on "foo").

        Thank you, I had an idea that it would be that simple but I was being a bit thick due to the lateness in the day. After working on these problems all day, the brain gets a little numb.

        Moriarty

Re: Finding a variable string within a variable string.
by dragonchild (Archbishop) on Dec 14, 2004 at 14:09 UTC
    If you want to determine "Does XYZ exist in the list ABC?", you want to store ABC as the keys in a hash. Then, lookup is very quick.

    Note: Re-read your post and try and look at it from the point of view we have - we don't work with you. This means we're missing a lot of the context you have when talking about your problem.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.