in reply to Skip lines until you find a delimiter >>

Guessing...

#!/usr/bin/perl # http://perlmonks.org/?node_id=1210866 use strict; use warnings; $/ = '>>'; <DATA>; $/ = '"'; chomp( my $variable = <DATA> ); print "variable = '$variable'\n"; __DATA__ <"Session Date:Mar 13/2017 ":>>test", 1,2,3,4 ":>>test1", 5,6,7,8 end>

Outputs:

variable = 'test'

Replies are listed 'Best First'.
Re^2: Skip lines until you find a delimiter >>
by RohanGuddad (Initiate) on Mar 14, 2018 at 08:14 UTC
    so this will skip first few lines and read from ">>"?

      Did on read the post or try the code provided?

Re^2: Skip lines until you find a delimiter >>
by RohanGuddad (Initiate) on Mar 14, 2018 at 14:41 UTC
    i want to sum all the numbers in a line.. except the first field.. could you please let me know how to do that... i am basically inserting data to 2 columns in a table.. the first field should be into 1st column and the sum of all values should be into 2nd column of my table. my data looks like below. <"Session" Attribute Date: 1/2/18, ">>:ABC",1,1.3,1,3,23, ">>:BCD",2,3,4,5,2, > So i need to load ABC to first column of table and next set of interger values to be summed and inserted to 2nd column of the table.
      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>
        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.