Help - Search - Member List - Calendar
Full Version: Multiple substitutions from a string into an array
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Mads N. Vestergaard
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Everybody,

I hope you can help me out a bit here.
I'm having some problems with multiple substitutions from a string.

My string is "<tag>foo</tag>test test test<tag>bar</tag>test test
test<tag>baz</tag>test test test".

What I would like to have is to get foo,bar,baz into an array, I've
tried to substitute it, but it doesn't seem to work for me. Either I get
foo correct and nothing else, or I get baz correct without anything else.

Hope you can help me out.

Thanks in advance.

Kind regards


Mads N. Vestergaard

- --
Mads N. Vestergaard - http://rwxr-xr-x.dk
Interested in Open Source, and web application development
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCyXEF7WOaPMd53OMRAiC+AJ92eE6Bvt347LkXxxnzcQhBApfpPgCg76Bl
k3e5670O0Q/6/zUB7+/vWuE=
=3YQN
-----END PGP SIGNATURE-----

Xavier Noria
On Jul 4, 2005, at 19:25, Mads N. Vestergaard wrote:

QUOTE
My string is "<tag>foo</tag>test test test<tag>bar</tag>test test
test<tag>baz</tag>test test test".

What I would like to have is to get foo,bar,baz into an array, I've
tried to substitute it, but it doesn't seem to work for me. Either
I get
foo correct and nothing else, or I get baz correct without anything
else.

The natural idiom there is m//g in list context:

my @tags = $string =~ m!<tag>(.*?)</tag>!g;

-- fxn

Ing. Branislav Gerzo
Mads N. Vestergaard [MNV], on Monday, July 04, 2005 at 19:25 (+0200)
wrote the following:

MNV> My string is "<tag>foo</tag>test test test<tag>bar</tag>test test
MNV> test<tag>baz</tag>test test test".

I hope this snippet will help you:

use strict;
use warnings;

my $string = qq{<tag>foo</tag>test test test<tag>bar</tag>test test
test<tag>baz</tag>test test test};

my @array = ();
while ( $string =~ /<([^>]+)>([^<]+)</1>/g ) {
push @array, $2;
}

print "@array";

....the main part is while() with regexp and "g" modifier.

--

...m8s, cu l8r, Brano.

[Unclog drains with Liquid Metal Terminator!]

Mads N. Vestergaard
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey,

Ah thanks, that's just what I needed :)

I haven't been using m and g together, that was the problem.'

Thanks alot.

Mads

Xavier Noria wrote:
| On Jul 4, 2005, at 19:25, Mads N. Vestergaard wrote:
|
|> My string is "<tag>foo</tag>test test test<tag>bar</tag>test test
|> test<tag>baz</tag>test test test".
|>
|> What I would like to have is to get foo,bar,baz into an array, I've
|> tried to substitute it, but it doesn't seem to work for me. Either I get
|> foo correct and nothing else, or I get baz correct without anything
|> else.
|
|
| The natural idiom there is m//g in list context:
|
| my @tags = $string =~ m!<tag>(.*?)</tag>!g;
|
| -- fxn
|
|

- --
Mads N. Vestergaard - http://rwxr-xr-x.dk
Interested in Open Source, and web application development
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFCyXiF7WOaPMd53OMRAhW4AJ9mfZ6b4affMfrcCy6g9G3CxtO03QCgw3DT
pUt7vEGhK883RYmTOblMa54=
=1F5z
-----END PGP SIGNATURE-----


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.