Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

ignoring blank lines

by vis1982 (Acolyte)
on May 25, 2010 at 17:49 UTC ( #841608=perlquestion: print w/ replies, xml ) Need Help??
vis1982 has asked for the wisdom of the Perl Monks concerning the following question:

file

ass ass

array

rat rasa

arrow ant

sad

question is if a file has both partners then we can print and leave out those lines which has one partner

So code is
# !usr|bin|perl -w open(FILE1,"$ARGV[0]"); @array1=<FILE1>; close(FILE1); foreach (@array1) { # next if ($_=~/(\s+$)/); my @t =split ("\s",$_); if ($t[0]=~/(\w*\d*)/) #(\s*)(\w*\d*)/){ { if ($t[1]=~/(\w*\d*)/) { #{ next if ($t[1]=~/(^\s+$)/); } print "$1 \n"; } } exit();
But it is not giving output

Comment on ignoring blank lines
Download Code
Re: ignoring blank lines
by toolic (Chancellor) on May 25, 2010 at 18:32 UTC
    Here are some suggestions which will help us answer your question:
    • use strict and warnings and clean up the messages that result.
    • Run your code through perltidy to clean up the indentation.
    • Post your expected output. I don't understand what you are trying to accomplish.
      A beginner needs to use strict;
      An expert wants to use strict;
Re: ignoring blank lines
by linuxer (Deacon) on May 25, 2010 at 18:36 UTC

    Hi, your shebang looks broken... Shouldn't it be #! |path|of|interpreter (1)?

    your subject says "ignoring blank lines", your text says "leave out those lines which has one partner"?

    So, what do you want to do?

    What do you mean with "partner"? Are you talking about words (partner == word)?
    Do you simple want to print those lines with two words?
    Maybe only those lines with two words, where the words are the same?

    The better you explain your problem, the better others can help you.

    Just a guess:

    #! /usr/bin/perl use strict; use warnings; # read magically from given files or STDIN; see perldoc perlop (null f +ilehandle) while ( my $line = <> ) { # split each line my @elements = split /\s+/, $line; # two words per line (and words are equal) if ( 2 == @elements && $elements[0] eq $elements[1] ) { print $line; } } # just for testing __DATA__ file ass ass array rat rasa arrow ant sad
    edit: (1): and I wonder whether there shouldn't be slashes /? Which OS uses pipe | as directory separator?
Re: ignoring blank lines
by BioLion (Curate) on May 25, 2010 at 18:46 UTC

    Not totally sure what you are after, but your code doesn't compare and isn't well laid out - see perlcritic and perlstyle. As a beginner (or anyone else) you should always use warnings and strict. You may also find diagnostics helpful - it will give you fuller explanations of warnings and their possible origin.

    Making a few guesses as to what you wanted (two identical elements per line?) I came up with the following:

    use warnings; use strict; use Data::Dumper qw/Dumper/; # very useful for debugging ++$|; my @array = (<DATA>); # check input print "INPUT :\n" . Dumper \@array; foreach my $line (@array){ chomp $line; # remove trailing newline # ignore empty lines next unless (defined$line && $line ne ''); # split on space my @t = split /\s+/, $line; # accept lines with two, identical elements if ( scalar @t == 2 && $t[0] eq $t[1] ){ print "MATCH : $line\n"; } else { print "NO : $line\n"; } } # gives - #INPUT : #$VAR1 = [ # 'file #', # ' #', # 'ass ass #', # ' #', # 'array #', # ' #', # 'rat rasa #', # ' #', # 'arrow ant #', # ' #', # 'sad' # ]; #NO : file #MATCH : ass ass #NO : array #NO : rat rasa #NO : arrow ant #NO : sad __DATA__ file ass ass array rat rasa arrow ant sad

    There are shorter ways to do this using lookaheads and regular expressions but it is usually best to stick with something clear! HTH.

    Just a something something...
Re: ignoring blank lines
by johngg (Abbot) on May 25, 2010 at 18:49 UTC

    You could do this on the command line, a script hardly seems necessary.

    $ cat spw841608.txt ile ass ass array rat rasa arrow ant sad jack sprat cat on a mat joe blow $ $ perl -ne 'print if 2 == split' spw841608.txt ass ass rat rasa arrow ant jack sprat joe blow $

    I hope this is helpful.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://841608]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-12-27 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls