Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Parse this 1

Status
Not open for further replies.

akrishmohan

Programmer
May 3, 2002
20
US
I have a data
[1 2 a b] that needs to be parsed to something like this
1 2 and a b ... How do I do it? Thanks for ur help
 
Whenever you post a problem like this, try your best to formulate it in perl. If you at least write out what your starting and ending conditions are, you at least have a chance of figuring it out yourself. And you also are more likely to communicate what you are wanting to the forum if you can't figure it out.

For example:

Code:
# I'm starting with the following data structure.
my @data = qw(a b 1 2);

# I need to separate the pure numbers from everything else.

# End:
my @numbers = qw(1 2);
my @other = qw(a b);

I'm going to assume that this is what you wanted to do. Therefore all you really need to know is how to test if something is a number or not.

Code:
my @data = qw(a b 1 2);

my @numbers;
my @other;

foreach (@data) {
    if (/^\d+$/) {
        push @numbers, $_;
    } else {
        push @other, $_;
    }
}

QED.
 
you'll need to be more descriptive for a start? is this in an array, string, ?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
You're question is very vague. Can you explain the parsing criteria better?



- Kevin, perl coder unexceptional!
 
MillerH, thanks for taking the time, * 4 that

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Well, sorry for being really vague.

I need to parse a file which contains a line of this pattern
[a b 1 2][a c 1 3]. Now I would need the output to print something like this
a b
1 2
a c
1 3

I would like to use only Regualar Expressions for this. Hope I am clear this time.

Thanks
 
akrishmohan:

Yes, that certainly contains more information. But once again, you aren't really being completely clear.

The best way to have us help you would be to give us a literal file. Or at least a truncated version. To be able to create a regular expression, we must be able to get a feel for what is constant and what is variable. Right now, all we have is one line, which we could choose to just treat as a literal.

Nevertheless. Making some more assumptions.

Code:
while (<DATA>) {
	if (/^\[(\w \w) (\d \d)\]\[(\w \w) (\d \d)\]$/) {
		print "$1\n$2\n$3\n$4\n";
	}
}

__DATA__
[a b 1 2][a c 1 3]
[c e 3 4][g h 2 1]

This is the last time I'm going to try making assumptions. If you really want help, take the time to actually write out in detail what you want, and the problem that you're having accomplishing it on your own.

Good luck.
 
got your spec, will send invoice upon confirmed payment ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Paul,

Thanks for the star. It really would be nice if there were some way to simply screen beggers. Unfortunately, I guess it's just going to be up to me to learn to tell the difference.

It wouldn't be so bad, if they were actually able to communicate what they wanted in detail. But I think that would require that they actually attempted to solve the problem themselves first before posting a question.

Shrug. [neutral]
 
Personally I also still think the question is still too vague but here goes:

Code:
$foo = '[a b 1 2][a c 1 3]';
$foo =~ tr/[]//d;
$foo =~ s/((\w \w)\s*(\d \d))/$2\n$3\n/g;
print $foo;

me too, no more assumptions, and please don't post homework assiggments if that's what this is.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top