Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: New to Perl

by nvivek (Priest)
on Jun 24, 2010 at 09:18 UTC ( #846260=note: print w/ replies, xml ) Need Help??


in reply to New to Perl

If you want to split the lines in an input file by using number of '*'.Then,you can use the regular expression to match the lines and push lines into two separate array.You check the following code.In this,I used both substitute as well as split function to do it.

use strict; use warnings; use Data::Dumper; open FH,"<input" or die "can't open:$!"; #opening the input file my(@arr1,@arr2); #declaring the arrays to store those lines while(<FH>) { #reading line from a file one by one if(/\*[^*]+\*/) { #checking whether the line has two *'s in it push @arr1,$_ ; #pushing that line to an array1 } else { push @arr2,$_; #pushing that line to an array2 if it isn't having two +*'s in it } } close FH; #closing the file handle print "Lines with two *'s in it\n"; print @arr1; print "Lines with two *'s in it\n"; print @arr2;

Another Way

open FH,"<input" or die "can't open:$!"; #opening the input file while(<FH>) { #reading line from a file one by one if((split '\*',$_)==3) #splitting the line by using '*' and checking w +hether the split returns three which means three filds in it { push @arr1,$_ ; #pushing that line to an array1 } else { push @arr2,$_; #pushing that line to an array2 if it isn't having thre +e fields in it } } print "Lines with two *'s in it\n"; print @arr1; print "Lines with two *'s in it\n"; print @arr2; close FH; #closing the file handle


Comment on Re: New to Perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-04-21 04:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls