http://qs321.pair.com?node_id=114203


in reply to Cropping the output of the pattern matcher

Parsing HTML may be tougher than it seems, so you might consider using HTML::Parser.

However, considering your question:

How do I crop the data based on my delimeters '><' and return only what was between them?

A simple split should suffice, such as:

my @bits_and_pieces = split /></, $line;

-- TMTOWTDI

Replies are listed 'Best First'.
Re: Re: Cropping the output of the pattern matcher
by jerrygarciuh (Curate) on Sep 24, 2001 at 00:29 UTC
    I will definitely take it under advisement and look into the modules others have developed for this porpoise,
    but in the spirit of learning please advise me on using split. I am now trying your code Trantor like so:
    #!/usr/local/bin/perl -w use strict; my $line = "a whole lotta shakin >pattern here< goin on"; my @bits_and_pieces = split /></, $line; print @bits_and_pieces;

    Done this way the out put is the same as $line was originally:
    a whole lotta shakin >pattern here< goin on
    But if I use
    my $line = "a whole lotta shakin ><pattern here>< goin on";
    Then my output is: a whole lotta shakin pattern here goin on
    The only thing that happened was my delimiters were deleted.
    Would some kind soul explain why split did that.
    I read perlfunc:split but I'm afraid I am no wiser.
    TIA,
    jg
      It seems as the delimiters were deleted, but the print is a little tricky

      First, you had the string:
      my $line = "a whole lotta shakin >pattern here< goin on";

      and you did
      my @bits_and_pieces = split /></, $line;

      What did it make?
      This code "splits" the string, searching '><' as delimiter. Because of '><' is not matched in the string, in @bits_and_pieces you will have only one element, and it would be
      'a whole lotta shakin >pattern here< goin on'
      (all the string)

      In the other hand, you did this
      my $line = "a whole lotta shakin ><pattern here>< goin on"; my @bits_and_pieces = split /></, $line;

      Then, perl searches '><' in the string and splits it into an array.
      So @bits_and_pieces would be now
      ('a whole lotta shakin ','pattern here',' goin on')

      If you print @bits_and_pieces with
      print @bits_and_pieces;
      is printed
      'a whole lotta shakin pattern here goin on'
      as if the delimiters '><' where removed from the array.

      Hope this help
      Hopes
      The only thing I might add to hopes fine reply to jerrygarciuh's question about split

      is that changing the final print might make the point clearer.....

      print "numboer of array elements==". scalar(@bits_and_pieces)."\n"; print join("\n",@bits_and_pieces);