Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Need help on extracting records !

Expand Messages
  • Charles K. Clarkson
    ... How do you know it didn t work? Did you test it by printing it? use Data::Dumper; print Dumper @read; chomp @read; print Dumper @read; You should
    Message 1 of 4 , Nov 1, 2001
    • 0 Attachment
      "eventualdeath" <eventualdeath@...> wrote:

      : I forgot to say that my whole idea was to
      : read the whole file frall.html into one line,
      : then replace horizontal line <hr> with <hr>\n
      : (horizontal line plus new line) then search
      : each records for jim.
      : My problem should be solved If I could read
      : the file frall.html into one line, I tried
      : using chomp to get rid of whatever-lines but
      : it is still there. Please advice.

      : I need members help on this:-

      : I wish to search a html guestbook for a
      : particular word lets say Jim and if the word
      : Jim is found , to display the records that
      : contain the word Jim. The format of the
      : guestbook is seperated by a horizontal line
      : & it goes something like this :-
      :
      : Name : Jim-could-be-here Age = their-age
      : Location :
      : Remarks : their remarks .....
      :
      : ------------------------------------------------------------
      :
      : Name : whatevername Age = their-age
      : Location :
      : Remarks : Jim-could-be-here-also
      : ------------------------------------------------------------
      :
      : and many more similar entries.....
      :
      : #######################
      : This is what I've tried but it still prints everything :-
      :
      : open (FILE, "frall.html")|| die"file not found $!\n";
      : @read = <FILE>;
      : chomp @read; # but it still could not get rid of the
      : # new-line (I dont know why?)

      How do you know it didn't work? Did you test it
      by printing it?

      use Data::Dumper;
      print Dumper \@read;
      chomp @read;
      print Dumper \@read;

      You should immediately notice the lack of newlines.

      : join ('<hr>\n', split(/<hr>/, @read));

      I like this approach, I don't think it would have
      occurred to me. I probably would have went with a
      regular expression and s///. As you mentioned in
      another post: "a good programmer must not have a one
      track mind." [sigh - some day] :)
      split doesn't work on lists, though. We could
      map it on or just flatten @read into a scalar. Please
      excuse my lack of parenthesis (I really despise them).
      Let's make the split case-insensitive while we're at
      it:

      @read = map "$_<hr>\n", split /<hr>/i, join '', @read;

      print Dumper \@read;

      Let me know if this worked.

      : foreach $record (@read){
      : if (grep /jim/, $record) {
      : print $record;
      : };
      : };

      Go back and reread how grep works:

      print grep /jim/, @read;


      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.


      The generation of random numbers is too important to be left to chance.

      As tested:

      #!/usr/bin/perl
      use strict;
      use warnings;
      use diagnostics;
      use Data::Dumper;

      open FILE, 'frall.html' or die "file not found $!";
      chomp (my @records = <FILE>);
      close FILE;

      @records = map "$_<hr>\n", split /<hr>/i, join '', @records;
      print Dumper \@records;

      print grep /jim/i, @records;

      __END__

      frall.html:
      <body><hr><P>Some trash for jim.</P>
      <HR>
      <P>Jim was here.</P>
      <hr><Hr><hr>
    • eventualdeath
      I have to Thank You Charles, Jenda and fellow Members once again!My original script works, no problem but it returns a blank page irregardless of
      Message 2 of 4 , Nov 1, 2001
      • 0 Attachment
        I have to Thank You Charles, Jenda and fellow Members once again!

        My original script works, no problem but it returns a blank page
        irregardless of whichever-name=> grep /jim/, $record <= I am searching.
        However it is able to search and returns the recordS accordingly if I
        replace this statement:-

        @read = join ('<hr>\n', split(/<hr>/, @read));

        with this statement, that's coded by Charles:-

        @read = map "$_<hr>\n", split /<hr>/i, join '', @read;


        > split doesn't work on lists, though. We could
        > map it on or just flatten @read into a scalar. Please
        > excuse my lack of parenthesis (I really despise them).

        I'm also quite bad at parenthesis, since I am still learning and have not
        studied "map", obviously I would like to know the things I must rectify to
        make the following statement equivalent to Charles, if not, it defeat my
        purpose of learning :-

        @read = join ('<hr>\n', split(/<hr>/, @read));


        Any advice would be very much appreciated.
        Thanks


        #!d:\perl\bin\perl.exe -w
        use CGI::Carp qw(fatalsToBrowser);
        print "cOnTenT-tYpE: text/html\n\n";

        open (FILE, "frall.html")|| die"file not found $!\n";
        @read = <FILE>;

        chomp @read;
        ### From Charles & it works #####
        ## @read = map "$_<hr>\n", split /<hr>/i, join '', @read;

        @read = join ('<hr>\n', split(/<hr>/, @read));

        foreach $record (@read){
        if (grep /jim/, $record) {
        print $record;
        }
        };

        ####### end of msg ######


        ----- Original Message -----
        From: "Charles K. Clarkson" <cclarkson@...>
        To: <perl-beginner@yahoogroups.com>
        Sent: Thursday, November 01, 2001 10:26 PM
        Subject: Re: [PBML] Need help on extracting records !


        > "eventualdeath" <eventualdeath@...> wrote:
        >
        > : I forgot to say that my whole idea was to
        > : read the whole file frall.html into one line,
        > : then replace horizontal line <hr> with <hr>\n
        > : (horizontal line plus new line) then search
        > : each records for jim.
        > : My problem should be solved If I could read
        > : the file frall.html into one line, I tried
        > : using chomp to get rid of whatever-lines but
        > : it is still there. Please advice.
        >
        > : I need members help on this:-
        >
        > : I wish to search a html guestbook for a
        > : particular word lets say Jim and if the word
        > : Jim is found , to display the records that
        > : contain the word Jim. The format of the
        > : guestbook is seperated by a horizontal line
        > : & it goes something like this :-
        > :
        > : Name : Jim-could-be-here Age = their-age
        > : Location :
        > : Remarks : their remarks .....
        > :
        > : ------------------------------------------------------------
        > :
        > : Name : whatevername Age = their-age
        > : Location :
        > : Remarks : Jim-could-be-here-also
        > : ------------------------------------------------------------
        > :
        > : and many more similar entries.....
        > :
        > : #######################
        > : This is what I've tried but it still prints everything :-
        > :
        >
        > : join ('<hr>\n', split(/<hr>/, @read));
        >
        > I like this approach, I don't think it would have
        > occurred to me. I probably would have went with a
        > regular expression and s///. As you mentioned in
        > another post: "a good programmer must not have a one
        > track mind." [sigh - some day] :)
        > split doesn't work on lists, though. We could
        > map it on or just flatten @read into a scalar. Please
        > excuse my lack of parenthesis (I really despise them).
        > Let's make the split case-insensitive while we're at
        > it:
        >
        > @read = map "$_<hr>\n", split /<hr>/i, join '', @read;
        >
        > print Dumper \@read;
        >
        > Let me know if this worked.
        >
        > : foreach $record (@read){
        > : if (grep /jim/, $record) {
        > : print $record;
        > : };
        > : };
        >
        > Go back and reread how grep works:
        >
        > print grep /jim/, @read;
        >
        >
        > HTH,
        > Charles K. Clarkson
        > Clarkson Energy Homes, Inc.
        >



        _________________________________________________________
        Do You Yahoo!?
        Get your free @... address at http://mail.yahoo.com
      • Charles K. Clarkson
        eventualdeath ... map is a one-line replacement for a for block and a temporary variable (like $read below): @read = map
        Message 3 of 4 , Nov 2, 2001
        • 0 Attachment
          "eventualdeath" <eventualdeath@...>

          : I have to Thank You Charles, Jenda and fellow Members
          : once again!
          :
          : My original script works, no problem but it returns a
          : blank page irregardless of
          : whichever-name=> grep /jim/, $record <= I am
          : searching. However it is able to search and returns
          : the recordS accordingly if I replace this statement:-
          :
          : @read = join ('<hr>\n', split(/<hr>/, @read));
          :
          : with this statement, that's coded by Charles:-
          :
          : @read = map "$_<hr>\n", split /<hr>/i, join '', @read;
          :
          :
          : > split doesn't work on lists, though. We could
          : > map it on or just flatten @read into a scalar. Please
          : > excuse my lack of parenthesis (I really despise them).
          :
          : I'm also quite bad at parenthesis, since I am still
          : learning and have not studied "map", obviously I would
          : like to know the things I must rectify to make the
          : following statement equivalent to Charles, if not, it
          : defeat my purpose of learning :-
          :
          : @read = join ('<hr>\n', split(/<hr>/, @read));
          :
          :

          'map' is a one-line replacement for a 'for' block
          and a temporary variable (like $read below):

          @read = map "$_<hr>\n", split /<hr>/i, join '', @read;

          might look like:

          {
          my $read = join '', @read
          @read = ();
          foreach (split /<hr>/i, $read) {
          push @read, "$_<hr>\n";
          }
          }

          Or (with a temporary list):
          {
          my @temp;
          foreach (split /<hr>/i, join '', @read) {
          push @temp, "$_<hr>\n";
          }
          @read = @temp;
          }

          HTH,
          Charles K. Clarkson
          Clarkson Energy Homes, Inc.


          It is a commonplace that the history of civilisation is largely the history
          of weapons.
          - George Orwell



          OT NOTE:
          The outside block {} is used to allow $read in
          the first example and @temp in the second example to
          go "out of scope". Perl may then be able to reuse that
          memory. (One day perl may be able to return freed
          memory to operating systems.) I believe scope is
          mentioned in perlsub.
        Your message has been successfully submitted and would be delivered to recipients shortly.