in reply to finding the field number of a matching pattern in perl

What do you mean field? col?

?? does this help Identifying the position of match using regular expressions

 if ($data1 =~ /Item | volume/) { ...

that can only match item or volume, not both

Replies are listed 'Best First'.
Re^2: finding the field number of a matching pattern in perl
by Anonymous Monk on Sep 16, 2012 at 09:09 UTC
    • #!/usr/bin/perl -- use strict; use warnings; my $data = "Region Item volume Month"; my @da = split /\s+/, $data; for my $daix ( 0 .. $#da ){ $_ = $da[$daix]; /volume/ and print "volume $daix\n"; /Item/ and print "Item $daix\n"; /(volume|Item)/ and print "$1 $daix\n"; } __END__
    • #!/usr/bin/perl -- use strict; use warnings; my $data = "Region Item volume Month"; my @da = split /\s+/, $data; my $daix = 0; for( @da ){ /volume/ and print "volume $daix\n"; /Item/ and print "Item $daix\n"; /(volume|Item)/ and print "$1 $daix\n"; $daix++; } __END__
    • #!/usr/bin/perl -- use strict; use warnings; use 5.012; my $data = "Region Item volume Month"; my @da = split /\s+/, $data; while( my($daix, $_) = each @da ){ /volume/ and print "volume $daix\n"; /Item/ and print "Item $daix\n"; /(volume|Item)/ and print "$1 $daix\n"; } __END__