Solved

# Regular expression

Posted on 2000-02-24
224 Views
I have a Txt file wich have a line like this:

{ELEM1,ELEM2,ELEM3}

This line can have as many ELEM? as I want, so I have to read a diferent number of elements each line.

For example:
Line1: {ELEM1}
Line2: {ELEM2,ELEM3}
Line3: {ELEM1,ELEM2,ELEM3}

How can I make a Regular expression so I can put in a Array the diferents elements.

Example:

The Regular expression....
Array[0]=ELEM1
Array[1]=ELEM2
......

0
Question by:Pampa

LVL 2

Accepted Solution

mattrope earned 100 total points
ID: 2554280
Try this (assuming the line is stored in \$line:

if (\$line =~ /\{(.*?)\}/) {
@array = split(',',\$1);
}

Hope this is what you were looking for.
0

Expert Comment

ID: 2554578
i hope that i have understood ur question.ur ultimate aim is to read lines and populate in to array which
has elem1 as first element,ELEM2 as second and so on
this should work though this is not the most effective way of doing it to my knowledge.u can make it more effective

while(\$line = <FD>) {#read line from file

@temp =  split(/,/\$line) ;
for(\$i=0;\$i<(\$#temp+1);\$++) {
if(\$temp[\$i] =` /^E.*?(\d)/{
\$array[(\$1-1)] = \$temp[\$i]
}
}
}
print @array;
i think u could do evenwithout splitting .i havent tried it out
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question