Perl question, parsing line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl question, parsing line by line
# 1  
Old 09-05-2008
Perl question, parsing line by line

Hello,

Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands.

Ive tried many methods however I cant get the last step.

$yourfile= 'FILENAME';#poker hands to parse
open (FILE, "$yourfile") or die $!;
@lines = <FILE>;

for (@lines)
{
if (/^Dealt.*]$/)
{
$blah = $_;
push (@cards,$blah);
}
}
print "@cards";

This script will produce the following output...

Dealt to me [4h 5s]
Dealt to me [Kh 2s]
Dealt to me [3c 6h]
Dealt to me [9s 8d]

I want to parse out the hands within the [ ], how do i do this?
# 2  
Old 09-06-2008
Code:
if (/^Dealt .*\[([^][]+)\]$/) {
  push @cards, $1;
}

Parentheses in a regular expression "capture" whatever matches the subexpression inside the parentheses, so you can refer to the first (counting opening parentheses from the beginning of the string) as $1, the second parenthesized subexpression match as $2, etc.

The regex looks a bit complex because the delimiters [ and ] are metacharacters, so they need to be backslashed, and there are a few uses of the actual metacharacters in that expression, too. Suppose the delimiters were < and > instead (these have no special meaning in regular expressions), the expression would be /^Dealt.*<([^><]+)>$/ which is at least a little bit easier to read. In so many words, inside the delimiters, use capturing parentheses, capturing as many as possible of any character except the delimiters (or newline, implicitly).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

3. Shell Programming and Scripting

Command Line Perl for parsing fasta file

I would like to take a fasta file formated like >0001 agttcgaggtcagaatt >0002 agttcgag >0003 ggtaacctga and use command line perl to move the all sample gt 8 in length to a new file. the result would be >0001 agttcgaggtcagaatt >0003 ggtaacctga cat ${sample}.fasta | perl -lane... (2 Replies)
Discussion started by: jdilts
2 Replies

4. Shell Programming and Scripting

Make Multile line is one line using window Perl

Hi All I have a .csv file which is showing data as ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours ... (7 Replies)
Discussion started by: adisky123
7 Replies

5. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

Perl question - How do I print contents of an array on a single line?

I have the following code: print @testarray; which returns: 8 8 8 9 How do I return the array like this: The output is: 8, 8, 8, 9 (5 Replies)
Discussion started by: streetfighter2
5 Replies

8. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

9. Shell Programming and Scripting

parsing command line switches in Perl

Hi, My perl script takes few switches which i'm parsing through GetOpt::Long module. My script looks like something : myscript.pl --file="foo" --or --file="bar" The --file switch takes 2 arguments foo and bar. The 2 values of file are separated by --or switch. I want to ensure that... (1 Reply)
Discussion started by: obelix
1 Replies

10. Shell Programming and Scripting

perl question - removing line from input file

In perl I want to do remove the top line of my input file then process the next line. I want to do something like head -1 inputfile > temp grep -v temp inputfile > newinputfile cp newinputfile inputfle is this possible in perl? (3 Replies)
Discussion started by: reggiej
3 Replies
Login or Register to Ask a Question