LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2010, 10:04 AM   #1
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Rep: Reputation: 1
Perl - How to stop range search at end of an array?


I have data that looks similar to this -

Quote:
John Smith (Address)
123 Main St
Unit 1
Los Angeles, CA 90201
Jane Smith (Address)
321 Elm St
Denver, CO 80201
So I need everything between each name, but I am not guaranteed that each time I match a name that I will have the same amount of lines, so I do a range pattern search line this to get all lines, no matter if there is 5 or 10 or 15. I simply do a loop that goes through the whole array until I hit the match, and this is my search pattern.

Code:
 if ($LINE =~ /^\s+$SELECTED_NAME/ ... $LINE =~ /\(Address\)/) {
        push @ADDRESS_INFO, $LINE;
        }
This works perfectly... until I hit the end and it doesn't get its final pattern match because it's at the end and there is no next entry with a (Address) line. So as a 'hack', I ended up inserting a final scalar at the end of the array that just says (Address) so it knows it's at the end. Ideally though, I'd like to do an "or" statement that says search for Address || return true if I hit the end of the array. How would I match on "End Of Array" essentially?

Devon
 
Old 12-07-2010, 10:22 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Code:
#! /usr/bin/perl -w
use strict;
my  %persons;
my $name;

    while( my $LINE = <> ){
        if( $LINE =~ m/(.+)\(Address\)$/ ){
            $name = $1;
            $persons{ $name } = ();
        }
        else{
            push @{$persons{$name}}, $LINE;
        }
    }

    foreach my $human ( keys %persons ){
        print $human, " $persons{$human}[0]\n";
    }

Last edited by theNbomr; 12-07-2010 at 10:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to print a range of a char array without a for loop? trist007 Programming 3 11-07-2010 09:15 AM
How do I search in a range of directories thorn101 Linux - Server 2 12-14-2009 06:30 AM
[SOLVED] sed print range to end of file schneidz Programming 4 04-08-2009 01:30 PM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration