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

grep Question

by Shivam05 (Initiate)
on May 20, 2015 at 07:06 UTC ( [id://1127218]=perlquestion: print w/replies, xml ) Need Help??

Shivam05 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to grep label(" in the array, it will generate error while executing . sample code:

$grepper=join('','label','(','"'); print "$grepper\n"; $array[0]="label("; $array[1]="label"; @LABEL = grep(/$grepper/, @array); print "@LABEL \n";

And also, i tried this line directly:

@LABEL = grep(/label\(\"/, @array); print "@LABEL \n";

if i use this it does not take " correctly in the label(". How can i over come the problemand solution for this.

--Shiva Prasad.

Replies are listed 'Best First'.
Re: grep Question
by Athanasius (Archbishop) on May 20, 2015 at 07:27 UTC

    Hello Shivam05, and welcome to the Monastery!

    Your code has two problems. First, you need to disable the metacharacters in $grepper when using it in a regular expression:

    my @LABEL = grep(/\Q$grepper\E/, @array);

    See quotemeta.

    Second, as others have noted, you set the value of $array[0] to label(, which does not contain the double-quote character. Therefore, the regular expression finds nothing to match in @array. You can fix this in several ways:

    $array[0] = "label(\""; $array[0] = 'label("'; $array[0] = qq{label("}; $array[0] = q{label("};

    See perlop#Quote-and-Quote-like-Operators

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: grep Question
by Happy-the-monk (Canon) on May 20, 2015 at 07:26 UTC

    To elaborate on Utilitarian's response:

    Your array contains both label" (with " ) and label( ( with ( ) .

    You fail to match label(" (with ( and " ) in the array because it is not there.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: grep Question
by GrandFather (Saint) on May 20, 2015 at 07:40 UTC

    Generally it helps us help you if you show the error generated by running the program.

    It will also help you track down problems if you always use strictures (use strict; use warnings;). I appreciate the code shown may be a fragment of something larger, but just to make sure you are aware of a couple of coding techniques, here's a "tidied" version of your code:

    use strict; use warnings; my $grepper = 'label("'; print "$grepper\n"; my @array = ("label(", "label"); my @LABEL = grep {/\Q$grepper\E/} @array; print ">@LABEL<\n";

    Note the updated final print to make it clear that the array is empty.

    Perl is the programming world's equivalent of English
Re: grep Question
by Utilitarian (Vicar) on May 20, 2015 at 07:20 UTC
    'label('  ne 'label("'
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: grep Question
by soonix (Canon) on May 20, 2015 at 10:30 UTC
    In case you want to match either ( or " (either/any of them), you can use a character class like this:
    use strict; use warnings; use Data::Dumper; my $grepper = 'label[("]'; my @array = qw ! label( label" label% label!; my @LABEL = grep /$grepper/, @array; print Dumper $grepper, \@array, \@LABEL;
    gives
    $VAR1 = 'label[("]'; $VAR2 = [ 'label(', 'label"', 'label%', 'label' ]; $VAR3 = [ 'label(', 'label"' ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1127218]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found