Howdy, Stranger!

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


Extracting a word from a line in an array

EmilyBEmilyB Member Posts: 1
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.

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

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


  • JonathanJonathan Member Posts: 2,914

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

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


    (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.