Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

Regex to match a line

by Win (Novice)
on Feb 03, 2006 at 16:08 UTC ( #527706=perlquestion: print w/ replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,
I'm having problems getting a regex to match the following line and split the colls into an array. Please insert the tabs where you would expect them.
OA P total M total F total M 0 M1-4 M 5-9 M 10-14 + M 15-19 M 20-24 M 25-29 M 30-34 M 35-39 M 40-44 + M 45-49 M 50-54 M 55-59 M 60-64 M 65-69 M 70-74 M +75-79 M 80-84 M 85+ F 0 F 1-4 F 5-9 F 10-14 F15- +19 F 20-24 F 25-29 F 30-34 F 35-39 F 40-44 F 45-49 + F 50-54 F 55-59 F 60-64 F 65-69 F 70-74 F 75-79 +F 80-84 F 85+
The regex I am using is:
if (my @match_A = $line =~ /^([^\t]+)\t #1 ([^\t]+)\t #2 ([^\t]+)\t #3 ([^\t]+)\t #4 ([^\t]+)\t #5 ([^\t]+)\t #6 ([^\t]+)\t #7 ([^\t]+)\t #8 ([^\t]+)\t #9 ([^\t]+)\t #10 ([^\t]+)\t #11 ([^\t]+)\t #12 ([^\t]+)\t #13 ([^\t]+)\t #14 ([^\t]+)\t #15 ([^\t]+)\t #16 ([^\t]+)\t #17 ([^\t]+)\t #18 ([^\t]+)\t #19 ([^\t]+)\t #20 ([^\t]+)\t #21 ([^\t]+)\t #22 ([^\t]+)\t #23 ([^\t]+)\t #24 ([^\t]+)\t #25 ([^\t]+)\t #26 ([^\t]+)\t #27 ([^\t]+)\t #28 ([^\t]+)\t #29 ([^\t]+)\t #30 ([^\t]+)\t #31 ([^\t]+)\t #32 ([^\t]+)\t #33 ([^\t]+)\t #34 ([^\t]+)\t #35 ([^\t]+)\t #36 ([^\t]+)\t #37 ([^\t]+)\t #38 ([^\t]+)\t #39 ([^\t]+)\t #40 ([^\t]+)\t #41 ([^\t]+)\t #42 # (.{1,}\t) #42 /x) {
The error message I get is:
Use of uninitialized value in pattern match (m//) at reformat_text_fil +e.pl line 125, <FILE> line 4.

Comment on Regex to match a line
Select or Download Code
Re: Regex to match a line
by Fletch (Chancellor) on Feb 03, 2006 at 16:16 UTC
Re: Regex to match a line
by murugu (Curate) on Feb 03, 2006 at 16:31 UTC

    Hi Win,

    What you need is split function.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: Regex to match a line
by davido (Archbishop) on Feb 03, 2006 at 16:53 UTC

    Here's a split example:

    my( @match_A ) = split /\t/, $line;

    Just a matter of using the right tool for the job.


    Dave

      Then you could verify if there are enough items on array:
      if ($#mach_A < 41) { # Less columns than expected. warn "Line has less columns than expected."; }

      Igor 'izut' Sutton
      your code, your rules.

        Would the following work the same way?
        if (@match_A < 41) { # Less columns than expected. warn "Line has less columns than expected."; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-04-19 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls