Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Store and match

by prasadbabu (Prior)
on Aug 11, 2009 at 06:26 UTC ( #787499=note: print w/ replies, xml ) Need Help??


in reply to Store and match

The mistake you have done is you have not stored the current line in variable $x. Also \w+ will match only 'note_values' and not the remaining string 'note_values,notes_values2 ' as it contains ',' in between.

The below code will meet your requirement. Also refer perlre to learn how to store the matched value in variables.

while($x=<DATA>){ if ($x =~ /^\[Note\](.+)$/){ $var = $1; #store in a variable print $var; } }

Prasad


Comment on Re: Store and match
Download Code
Re^2: Store and match
by Anonymous Monk on Aug 11, 2009 at 06:37 UTC
    #!/usr/bin/perl while($x=<DATA>){ if ($x =~ /^\[Note\](.+)$/){ $var = $1; #store in a variable print $var; } } __DATA__ [Note] note_values,notes_values2 [Book] novels,magazines [Note] note_values,notes_values3
    Hi, if the values are as above, then how to print the values of Note. If the values of Note has "note_values", then how to count the number of occurence

      I am not clear with your inputs because in your previous node you gave [Note] in same line and now in two lines. Any how the below code will work for your above input. It will also match note_values if more than one present in a single line.

      use strict; use warnings; my $prev = 0; my $total = 0; while(my $x = <DATA>){ $prev = 1 if ($x =~ /^\[Note\]$/); my $count = 0; if (($x =~ /note_values/) && $prev){ $count = $x =~ s/(note_values)/$1/g; #if more than one note_va +lues present in a line $prev = 0; } $total = $total + $count; } print "Number of occurence: $total";

      Prasad

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://787499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2015-07-07 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls