Piping in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Piping in Perl
# 8  
Old 09-03-2008
Hi radoulov,

Seems that the below Perl code, even without array term, it doesn;t work.
Can you help ?


Input File:

AAA GGG TTT
BBB TTT FFF
AAA BBB TTT YYY
AAA BBB


Shell
Code:
$ cat file| grep AAA| grep BBB
AAA BBB TTT YYY
AAA BBB

Perl
Code:
#!/usr/bin/perl

open(FH, "< file");
        while (<FH>) {
                if(/AAA/ && /BBB/ ) {
                        print $_ ; print "\n"; last ;};
        };
close FH;

# 9  
Old 09-03-2008
Why you're using last?

Code:
zsh-4.3.4% cat file
AAA GGG TTT
BBB TTT FFF
AAA BBB TTT YYY
AAA BBB
zsh-4.3.4% cat p
#!/usr/bin/perl

open(FH, "< file");
        while (<FH>) {
                if(/AAA/ && /BBB/ ) {
                       print }
        };
close FH;
zsh-4.3.4% ./p
AAA BBB TTT YYY
AAA BBB

And as I said, you could write it as:

Code:
zsh-4.3.4% perl -lne'print if /AAA/ and /BBB/' file
AAA BBB TTT YYY
AAA BBB

# 10  
Old 09-03-2008
Hi radoulov,

Thanks for the guidance!
It's working now.

However, I am confused about the below array syntax.
Which is exactly the right one if we want to know the value.

@array[$num] VS $array[$num]
# 11  
Old 09-04-2008
Quote:
Originally Posted by Raynon
Hi radoulov,
However, I am confused about the below array syntax.
Which is exactly the right one if we want to know the value.

@array[$num] VS $array[$num]
Certainly $array[$index] is the correct way of referring to a single element of the array.
As a reminder, think of what you want to obtain; a single value, thus use the sigil $.
The other notation @array[@index_list] is an array slice.
Here you usually want to get several array elements, thus use the sigil @.
Though @array[$index] still would work, it is considered bad style
because you are after only a single element and thus there's no need for a slice.
As this is confusing a lot of Perl beginners, the language designers of Perl 6
I think will change the syntax to the sigil @ even when referring to a single element.
But I am not sure since Perl 6 is still work in progress.
And we only know that it will be out by Xmas, but we still don't know which Xmas.
But once it is out every day will be like Xmas.
# 12  
Old 09-07-2008
Hi Buffoonix,

Thanks alot for sharing!!
Appreciate that !
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (6 Replies)
Discussion started by: frymor
6 Replies

3. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (1 Reply)
Discussion started by: frymor
1 Replies

4. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

5. UNIX for Dummies Questions & Answers

Piping GREP

Hi, I need to use a double grep so to speak. I need to grep for a particular item say BOB and then for each successful result I need to grep for another item say SMITH. I tried grep "BOB" filename | grep "SMITH" but it does not seem to work. I can achieve my desired result using an... (12 Replies)
Discussion started by: mojoman
12 Replies

6. Programming

Piping Question

I have a piping question, I am trying to implement piping on my own shell and am having some trouble...esentially I am trying to make something to do command|command|command. I can get it to work fine if the last pipe command is not forked, but executes in the shell and then exits..but I need it... (2 Replies)
Discussion started by: mtobin1987
2 Replies

7. UNIX for Dummies Questions & Answers

Piping in UNIX

All, I am a UNIX novice with a question that I hope you can help me with. I have a UNIX application called "Tole" that formats and displays specific information about customers. I can display the information for up to 30 customers by seperating customer IDs using commas in this format: Tole -c... (3 Replies)
Discussion started by: simo007
3 Replies

8. Shell Programming and Scripting

Piping from device?

Hi Long time since I did any shell scripting so please be gentle with me! :) Just wanted to know whether it is possible to take the streaming output from a dvb card /dev/dvb/adapter0/ and using named pipes and tee to pass the outputs to mplayer and mencoder so as to watch and record a telly... (0 Replies)
Discussion started by: gary101
0 Replies

9. Shell Programming and Scripting

piping

I am using pipes (specifically piping out) in Perl to put an array from one file into an array in a different file. I can't figure out how to transfer the array. I kow how to open the pipe : open (FILEHANDLE, "| file") or die~ but how do I transfer the array. I think it has something to do with... (1 Reply)
Discussion started by: lnatz
1 Replies

10. Shell Programming and Scripting

Help (Piping ls, tr, cut)

I have to: pipe ls, tr, and cut to output the size (in bytes) and name of all of the files/dirs in the current directory (including any hidden ones), with the size at the beginning of the line, followed by a single tab character, followed by the the filename. I don't know what the point of... (2 Replies)
Discussion started by: scan
2 Replies
Login or Register to Ask a Question