Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

store rest of the lines in a array after a matching pattern is found

by Anonymous Monk
on May 17, 2018 at 17:22 UTC ( #1214764=perlquestion: print w/replies, xml ) Need Help??

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

Im trying to match a pattern from a line in file and then print rest of the line to an array split by comma and print elements of it

So I tried a perl code
#!/usr/bin/perl my $str="ServiceCategoryName=(3,1,0,1,1,0)"; if ( $str =~ m/^ServiceCategoryName=/) { printf($str); my @array=split(/,/$str); printf("my string ".$array[0]); }
  • Comment on store rest of the lines in a array after a matching pattern is found
  • Download Code

Replies are listed 'Best First'.
Re: store rest of the lines in a array after a matching pattern is found
by roboticus (Chancellor) on May 17, 2018 at 17:29 UTC

    You don't specifically ask a question, but since you say "rest of the line", I'm guessing your problem is that you're printing too much after "my string".

    I'd suggest changing your regular expression to:

    if ( $str =~ m/^ServiceCategoryName=(.*)/) {

    which will, if the expression matches, put the rest of the line (i.e., the stuff after the '=') into the $1 capture buffer. From there you could extract the stuff you're interested in, like so:

    if ( $str =~ m/^ServiceCategoryName=(.*)/) { print $str; # Extract the array from "the rest of the line" my @array = split /,/, $1; print "my string ", $array[0], "\n"; }

    Notes:

    • You're not providing a format string, so I changed printf to print.
    • You should use cut & paste to put your actual code, rather than retyping it. That'll help you avoid typos, like the missing comma in your split statement.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Tried that option earlier but got an error Unmatched ( in regex; marked by <-- HERE in m/^ServiceCategoryName=( <-- HERE .*/ at

        Then you had a syntax error in your code (such as either missing the closing parenthesis, misplacing it (putting it to the right of the slash), or accidentally escaping it (by preceding it with a \). As you can see here, it should just work:

        $ perl foo.pl ServiceCategoryName=foo,bar,baz my string foo $ cat foo.pl use strict; use warnings; my $str = "ServiceCategoryName=foo,bar,baz\n"; if ( $str =~ m/^ServiceCategoryName=(.*)/) { print $str; # Extract the array from "the rest of the line" my @array = split /,/, $1; print "my string ", $array[0], "\n"; } else { print "Can't find ServiceCategoryName!\n"; }

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Tried that option ...

        You don't say what option you tried (a mortal sin in the Monastery), but one can make a guess at your code:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $str = 'ServiceCategoryName=(3,1,0,1,1,0)'; ;; if ($str =~ m/^ServiceCategoryName=(.*/) { print qq{'$str'}; my @array = split /,/, $str; print qq{first split element: '$array[0]'}; } " Unmatched ( in regex; marked by <-- HERE in m/^ServiceCategoryName=( < +-- HERE .*/ at -e line 1.
        roboticus has suggested ways to fix this:
        c:\@Work\Perl\monks>perl -wMstrict -le "my $str= 'ServiceCategoryName=(3,1,0,1,1,0)'; ;; if ($str =~ m/^ServiceCategoryName=\((.*)\)/) { print qq{parenthesized substring: '$1'}; my @array = split /,/, $1; print qq{first split element: '$array[0]'}; } " parenthesized substring: '3,1,0,1,1,0' first split element: '3'
        In future, please show exactly what you have tried, the exact output you got, and what you wanted. Please see Short, Self-Contained, Correct Example.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2020-11-30 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?