Extracting a word from a line in an array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Extracting a word from a line in an array

EmilyBEmilyB Posts: 1Member
Hello. I am new to Perl programming and I have what may be a simple question! I need to write code to remove a number from a line in a file. The number is never in exactly the same spot and they are not the same length but it always has a word right in front. For example:

dogs Dachshund X732i AnimalNum:[b]Q21097[/b] ens2 trip1
cats kitty z898 AnimalNum:[b]IS2193[/b] arg32 bfn1

The portion I'm looking for is in bold.

I have code that reads in the file into an array and then it searches for say Dachshund or kitty and finds the line that has that entry and then I need it to give me the AnimalNum.

[code]
foreach my $line (@animal_list){
if ($line =~ /$var/){ #var1 = Dachsund or kitty
if ($line =~ /$var2/){ #var2 = AnimalNum

print OUTPUTF "$line
";
#now I need to find the AnimalNum that is in $line
}
[/code]

How would I search for the word AnimalNum and then take the ID number directly after it?

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    You need to use parens to write a capture. Something like:

    [code]$line =~ /AnimalNum: (S+)/;
    my $number = $1;[/code]

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Sign In or Register to comment.