-4

I've got a list like this:

Bicycles: Childrens  
289  
Bicycles: Mountain Bikes  
928  
Bicycles: Road Bikes  
870  
Camping & Outdoors Equipment  
761  
Canoes, Kayaks, Row-Boats  
231  
Climbing Equipment  
120  
Freeweights and Home Gyms  
583  
GPS and Locators  
104  
Golf Equipment  
1,223  
Other Fitness Equipment  
668  

I just need to skip lines that are numbers or blank

if (($line =~ /0..9/) || ($line eq "")){}  
else{  
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
t a
  • 191
  • 1
  • 3
  • 14

4 Answers4

1
print $line if $line !~ /[0-9]/ and $line =~ /\S/;

perldoc perlre. You'll learn more if you put some effort into it.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • :-) if (( $line =~ /[0-9]/ or $line eq "")){} – t a Apr 14 '12 at 16:31
  • Please explain what you mean by that comment using English. I wrote the line in my answer for a specific reason: 1) I do not know if you `chomp`ed input 2) You use the word ***blank*** referring to a line and a line containing nothing but blanks is still a blank line to me, and 3) I do not like empty blocks following `if`s. The line above says "`print` if the line does not contain any digits and it contains some non-space characters. If you need something else, say so. – Sinan Ünür Apr 14 '12 at 17:31
  • I'm sorry. I just modified your answer to work with my code. It's not a crucial script. It worked great. Thanks – t a Apr 14 '12 at 18:15
1

If you write

while (<>) {
  next unless /[^\d,\s]/;
    :
}

your program will skip all lines that contain only digits, commas, and whitespace.

This program shows the idea

use strict;
use warnings

while (<DATA>) {
  next unless /[^\d,\s]/;
  print;
}

__DATA__
Bicycles: Childrens  
289  
Bicycles: Mountain Bikes  
928  
Bicycles: Road Bikes  
870  
Camping & Outdoors Equipment  
761  
Canoes, Kayaks, Row-Boats  
231  
Climbing Equipment  
120  
Freeweights and Home Gyms  
583  
GPS and Locators  
104  
Golf Equipment  
1,223  
Other Fitness Equipment  
668  

output

Bicycles: Childrens  
Bicycles: Mountain Bikes  
Bicycles: Road Bikes  
Camping & Outdoors Equipment  
Canoes, Kayaks, Row-Boats  
Climbing Equipment  
Freeweights and Home Gyms  
GPS and Locators  
Golf Equipment  
Other Fitness Equipment  
Borodin
  • 126,100
  • 9
  • 70
  • 144
0
if ($line !~ /^[\d,]*[\n\r]*$/) {
  # do something
}   
else {
  # blank or "digits & comas" only line
}

By "blank" I mean empty string with optional end of line (in case you don't use chomp)

Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

next if (!$line || $line != int($line));

First is blank values check, the second one checks whether this value is integer or not.

Shay
  • 41
  • 3