open DAT, "ads.dat" or die "Error: cannot open data file: $!\n"; @imgdata=; @imgs = grep { s/^[^|]+\|([^|]+)\|[^|]+\|(?:internal|partner)$/$1/ } @imgdata; $img = $imgs[int(rand($#imgs+1))]; print "Location: $img\n\n"; #### s/^ # Beginning of string [^|]+ # Everything up to the next bar \| # The bar ( # Capture to $1 [^|]+ # Everything up to the next bar ) # End capture \| # The bar [^|]+ # Everything up to the next bar \| # The bar (?: # Non-capturing parens internal # the string "internal" | # or partner # the string "partner" )$ # End of string /$1/x # Substitute $1 for string