Perl : Search for next occurence of a word in an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : Search for next occurence of a word in an array
# 1  
Old 09-16-2009
Perl : Search for next occurence of a word in an array

I have an array as follows:

Space: ABC
Name: def
Age: 22
Type: new
Name: fgh
Age: 34
Type: old
Space: XYZ
Name: pqr
Age: 44
Type: new
:
:

How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on...
# 2  
Old 09-16-2009
Something like this:
Code:
$ cat test.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $i=-1;
my @array;

while(my $line=<DATA>){
        chomp $line;
        $i++ if $line=~/^Space/;
        push @{$array[$i]},$line;
}

print Dumper(\@array);

__DATA__
Space: ABC
Name: def
Age: 22
Type: new
Name: fgh
Age: 34
Type: old
Space: XYZ
Name: pqr
Age: 44
Type: new
$ perl test.pl
$VAR1 = [
          [
            'Space: ABC',
            'Name: def',
            'Age: 22',
            'Type: new',
            'Name: fgh',
            'Age: 34',
            'Type: old'
          ],
          [
            'Space: XYZ',
            'Name: pqr',
            'Age: 44',
            'Type: new'
          ]
        ];

To get line one of element 1, use $array[0][0], for line 3 of element 2 use $arary[1][2], ...
# 3  
Old 09-16-2009
Thanks Pludi for the quick reply.

One thing I forgot to mention earlier is that the Data which I posted is from an array & is not definite. I think there might be some changes need to be done then. Can u pl help me understand on how can I achieve this ?
# 4  
Old 09-16-2009
Quote:
Originally Posted by deo_kaustubh
[...]One thing I forgot to mention earlier is that the Data which I posted is from an array & is not definite.[...]
What do you mean by that? The code above will work as long as each section starts with 'Space' at the beginning of a line.

Or do you mean that you've already got the data in an array? In that case, just replace
Code:
while(my $line=<DATA>){

with
Code:
foreach my $line(@yourarray){

# 5  
Old 09-16-2009
Thanks pludi, it seems to be working... will I b able to use @array as a normal array for further processing ??
Just for my understanding can u explain me the use of Dumper which is being used in this code (m pretty new to perl).

Last edited by deo_kaustubh; 09-16-2009 at 12:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Search and replace a array values in perl

Hi, i want to search and replace array values by using perl perl -pi -e "s/${d$i]}/${b$j]}" *.xml i am using while loop for the same. if i excute this,it shows "Substitution replacement not terminated at -e line 1.". please tell me what's wrong this line (1 Reply)
Discussion started by: arindam guha
1 Replies

2. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

3. UNIX and Linux Applications

Perl Script to read an excel file into an array and search in the UNIX directories

Hi, I want the Perl script with versions 5.8.2 and 5.8.5 starting with #!/usr/bin/perl The Perl program should read the excel file or text file line by line and taking into an array and search in the UNIX directories for reference file of .jsp or .js or .xsl with path .The Object names... (2 Replies)
Discussion started by: pasam
2 Replies

4. Shell Programming and Scripting

Search Files from Array and link to original location in Perl

Hello, Question is related to Perl: I need to search few of the files from the array of file names. And after grepping the file names from an array I need to link these files to original location. The original location in this case is ref_path as input from the user. ##$ref_path is... (3 Replies)
Discussion started by: aarora1
3 Replies

5. Shell Programming and Scripting

Perl Regular Expression Word Search

I'm trying to have my perl script telnet into the network device execute a command then dump the output of the command into a variable. The script then greps for the word "STANDBY". I can't seem to get the script to print out the output because it seems that the script can't find the word... (1 Reply)
Discussion started by: xmaverick
1 Replies

6. Shell Programming and Scripting

search of string from an array in Perl

Hi All I want to search a string from an array in Perl. If a match occurs, assign that string to a variable else assign 'No match'. I tried writing the script as follows but it's in vain. Please help me.. #!/usr/bin/perl use strict; my $NER; my @text=("ORG","PER"); ... (4 Replies)
Discussion started by: my_Perl
4 Replies

7. UNIX for Dummies Questions & Answers

counting the occurence of a word

In a file I have to count a particular word. like i need apache how many times. I tried this $ tr "\011" "\012\012"<foo1 | tr -cd "" |sort\uniq -c but I got result like this 32 apache 18 dns 12 doctor Please sugest me (4 Replies)
Discussion started by: pranabrana
4 Replies

8. Shell Programming and Scripting

Perl: array, assigning multi-word sentences with quotes

Just wondering if there's a better way to get these complete sentences into an array and keep the quotes intact? All the quotes make it look ugly to me but it works. I want to be able to refer to the full sentences by index. I've tried a few qw and qq/ aproaches but what I have below seems about... (4 Replies)
Discussion started by: gctaylor
4 Replies

9. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

10. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies
Login or Register to Ask a Question