Regex to match a line
Win
created: 2006-02-03 11:08:45
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_file.pl line
125,  line 4.
Re: Regex to match a line
created: 2006-02-03 11:16:46

Erm, split?

Re: Regex to match a line
created: 2006-02-03 11:31:57

Hi Win,

What you need is split function.

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

Re: Regex to match a line
created: 2006-02-03 11:53:22

Here's a [doc://split] example:

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

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


Dave

Re^2: Regex to match a line
created: 2006-02-03 12:24:49
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.

Re^3: Regex to match a line
Win
created: 2006-02-07 12:07:44
Would the following work the same way?
if (@match_A < 41) {
    # Less columns than expected. 
    warn "Line has less columns than expected.";
}
Re^4: Regex to match a line
created: 2006-02-07 12:18:48
From perldoc perldata:
scalar(@whatever) == $#whatever + 1;

You should use 42 instead 41, like the code below:

if (@match_A < 42) {
    # Less columns than expected.
    warn "Line has less columns than expected.";
}

Igor 'izut' Sutton
your code, your rules.

perlmonks.org content © perlmonks.org and davido, Fletch, izut, murugu, Win

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03