in reply to Re: Extract varibles from 2 text files and make a report
in thread Extract varibles from 2 text files and make a report

If the File B like:
Group Name: Small
Junk: lll
Product Name:
AAA "This is test1 for me."
BBB "This may be I like it."
Group Name: Big
Junk: ggg
Product Name:
AAA "This is t2."
CCC "This should be ok"
DDD "This is the last one. we shoud have more."

How do I split the string of each product? The output is same as before.

  • Comment on Re^2: Extract varibles from 2 text files and make a report

Replies are listed 'Best First'.
Re^3: Extract varibles from 2 text files and make a report
by JediWizard (Deacon) on Oct 26, 2004 at 18:13 UTC

    If your goal is to discard everything following "AAA" or "BBB", and so on, you could modify the split statement to look like this:

    @prods = split(/\s[^\n]+\n/, $prod);

    This will remove everything following the first whitespace character in each of the lines following "Product Name:". Is that what you were trying to do?

    May the Force be with you
      That is what I want. Thanks. But if there is some spaces infront of AAA and BBB what should I do?
      Thank you!!!

        For info on using split see split.

        Hmmm... Spaces at the begining of the line... At this point it may be better to simply split on "\n" and then parse the line... This is a confusing line of code:

        @prods = map({/(\S+)/} split(/\n/, $prod));

        So I'll also give it to you in a more broken out way.

        @prods = split(/\n/, $prod); foreach my $line (@prods){ $line =~ s/^\s*(\S+).*$/$1/; }

        But it might be better to do it like this:

        @prods = ($prod =~ m/(?:\A|\n)\s*(\S+)/g);

        Yeah, I think I like the last way the best. It is probably faster too, but I haven't tested that.

        May the Force be with you
      But if there are some spaces before "AAA" or "BBB" what should I do. How do you find the reference on using "split"? Thank you!