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

Hi, I am extremely new to Perl. I have been reading and teaching myself alot. I am using a windows OS

I am trying to read from a file and search for a phrase that equals another phrase, then print it.
I know how to open the file and write/read from it but I dont know how to use the split command or how to search for the phrase that I want.

Example of what is in the text file:
hi==hello

I want to search for hi then print what hi equals. If somone could help me I would greatly apreciate it.

Replies are listed 'Best First'.
Re: reading from a file and splitting
by jweed (Chaplain) on Jan 17, 2004 at 00:52 UTC
    This sounds homework-y, so I'll get you started. Your OP is not so detailed so I'm grasping at straws for some of this. ;)
    Step 1: Make a hash like this:
    while(<>) { ($key, $value) = split(/==/); $hash{$key} = $value; }

    Step 2: lookup the string you want with your hash by using it as a key.
    Step 3: print what the hash spits out as its value.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
Re: reading from a file and splitting
by b10m (Vicar) on Jan 17, 2004 at 00:53 UTC

    First of all, you need to open the file in question, after that, you need to use regular expressions to match the thing you're looking for (please do follow that link and read the POD). And after all, close the file. This all can look like this:

    open(FILE, "<$file") || die ("Can't open $file: $!"); while(<FILE>) { print $1 if ($_ =~ /^hi==(.*)/); } close FILE;

    A little more information on this. Line one opens the file, specified in $file, or dies with the error message. Line to says: while we loop through the lines of the file, do this block (that starts with { and ends with }). Line three is your magic.See the documentation for more on this "magic". It states: print $1 (the result of the coming lookup) if the current line ($_) equals "hi==" and anything after that will be what we want ($1). And of course, we close the block and file after that.

    HTH

    Update: reformated several things, for I can not read questions, or so it seems :(

    --
    b10m
Re: reading from a file and splitting
by flatline (Novice) on Jan 17, 2004 at 01:37 UTC
    You could also access the data with a regex like this:

    /==/

    Then, you've broken the line into three parts: the part before the ==, the ==, and the part after. Assuming that == matches in the line, you can access these parts thus:

    $` = the part before the ==

    $& = "=="

    $' = the part after the ==

    the first example of split may be more appropriate for this type of thing though.

    okay apparently I can't read questions either. Didn't see you _just_ wanted string(s) beginning with hi. My example would be more appropriate if, for some reason, you wanted this plus other, similarly formatted data;)

      Since the use of these three variables causes such a huge program-wide performance hit, I would definately shy away from using them in a situation like this where split is so ideal.



      Code is (almost) always untested.
      http://www.justicepoetic.net/
        thank you, this is what i ended up with:
        open (file, "<file.txt"); @file = <file>; close (file); foreach $line(@file){ ($var1, $var2) = split(/\=\=/, $line); if($chat =~ /\b$var1\b/i){ #the item i was looking for print "$var2"; }