in reply to Re^2: Skip lines until you find a delimiter >>
in thread Skip lines until you find a delimiter >>

Your question is still not completely clear to me. A good tool for this job is "regular expressions" or regex for short. If the regex succeeds, the left hand side assignment is defined otherwise it is not.

As an update note: If for some reason this doesn't do exactly what you want, I expect you to spend some time studying the code and for you to make a serious attempt at modifications. I think you are doomed if you just copy code without understanding how it works.

#!/usr/bin/perl use strict; use warnings; use List::Util qw(sum); while (<DATA>) { my ($var) = /:\>\>(\w+)/; # could have been done my (@nums) = /,\s*(\d+)/g; # on one line... next unless $var; # skips bogus lines print "$var,",sum(@nums),"\n"; } #prints: #test,10 #test1,26 __DATA__ <"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end>

Replies are listed 'Best First'.
Re^4: Skip lines until you find a delimiter >>
by RohanGuddad (Initiate) on Mar 15, 2018 at 13:56 UTC
    thank you for the reply.. it was bit urgent.. if i want to sum positive and negative numbers in the same line.. how would i do that..? Currently the code is summing only the positive numbers. it would be really helpful if you can suggest on that.

      I take it you didn't read this part:

      "As an update note: If for some reason this doesn't do exactly what you want, I expect you to spend some time studying the code and for you to make a serious attempt at modifications. I think you are doomed if you just copy code without understanding how it works."

      This isn't a code writing service, the idea is that you learn from the help you've been given. You've been drip feeding different requirements since your initial post. Read and understand How do I post a question effectively? & tutorials->PerlMonks for the Absolute Beginner.

      "...it was bit urgent..."

      Yes, sure. I know. It's always the same - hard job etc. Such is life.

      BTW: May i kindly ask if you are aware that there are no SLAs on PM?

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      I suggest that you study this line:
      my (@nums) = /,\s*(\d+)/g;
      It could very well be that it takes you all day to come close to understanding what that does, but such is the nature of learning something that is both new and complicated.

      The major failures in your posting(s): Incomplete/inadequate specification of the problem. And apparently a complete lack of effort on your part to learn from the Monk replies. This last part is the issue that is causing the most problems.

      Your problem can be solved easily with a combination of a text editor and Excel. If you are not willing to put in the effort to learn Perl, then just ask a friend in the accounting department to whip out an answer with off-the-shelf software.

      Update:
      Ok, I don't want to torture you with something that is obvious to the average Monk. A simple way is:

      my (@nums) = /,\s*([-\d]+)/g;
      In the future, please understand that PerlMonks is about learning Perl.