substitute a string in array 
Author Message
 substitute a string in array

Hello,
I'm reading a file into an array then splitting it:

open(INFILE, '<test.txt')||die "Error opening file.$!\n";
while (<INFILE>)

.
.

Quote:
}

I need to substitute a string in that array, such string like " > " to be
substituted with " &gt; ". Tried to look in newsgroups and online without
good help.

Thanks for any help

Sam



Wed, 11 Aug 2004 02:14:59 GMT  
 substitute a string in array
open(INFILE, '<test.txt')||die "Error opening file.$!\n";
while (<INFILE>)

$_ =~ s/\</\&g\t;/g;
Quote:
}

--

http://ndream.vgamin.com


Quote:
> Hello,
> I'm reading a file into an array then splitting it:

> open(INFILE, '<test.txt')||die "Error opening file.$!\n";
> while (<INFILE>)
> {

> .
> .
> }

> I need to substitute a string in that array, such string like " > " to be
> substituted with " &gt; ". Tried to look in newsgroups and online without
> good help.

> Thanks for any help

> Sam



Wed, 11 Aug 2004 09:50:02 GMT  
 substitute a string in array

Quote:
> I need to substitute a string in that array, such string like " > " to be
> substituted with " > ". Tried to look in newsgroups and online without
> good help.

If you need to [en|de]code HTML entities (like >), have a look at the
HTML::Entities module.

--felix



Wed, 11 Aug 2004 11:58:56 GMT  
 substitute a string in array

Quote:

>I'm reading a file into an array then splitting it:

                                  ^^^^^^^^^^^^^^

You can substitute in a scalar ($_) _before_ you split it into the array.

You substitute in a scalar with the s/// operator.

Quote:
>I need to substitute a string in that array,

If you _did_ need to substitute in an array, you can do that with
the aliasing feature of the foreach loop:


     #  s/// as below
   }

When you modify the loop control variable ($_ in this case),
the modification is made back in the array being foreach'd.

Read about foreach loops with:

   perldoc perlsyn

Quote:
>such string like " > " to be
>substituted with " &gt; ".

If what you are really doing is escaping HTML entities, then
you should use a module that does that.

Assuming that you are NOT escaping entities and just want
to do a substitution:

   s/ > / &gt; /g;

will do it.

Quote:
>Tried to look in newsgroups and online without
>good help.

The s/// operator is documented on your very own hard disk:

   perldoc perlop

and the regular expressions that the operator uses are described here:

   perldoc perlre

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 11 Aug 2004 17:45:07 GMT  
 substitute a string in array

Quote:

>while (<INFILE>)
>{

>$_ =~ s/\</\&g\t;/g;
>}



He wants to substitute only when bracketed by space characters.

Your code does not substitute only when bracketed by space characters.

He wants to replace with ampersand-g-t-semicolon.

Your code replaces with ampersand-g-tab-semicolon.

None of the backslashes you have used are needed, and one of
them breaks stuff.

[snip upside-down quote of entire article, please don't do that]

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Wed, 11 Aug 2004 17:45:07 GMT  
 substitute a string in array

Quote:

> > Hello,
> > I'm reading a file into an array then splitting it:

> > open(INFILE, '<test.txt')||die "Error opening file.$!\n";
> > while (<INFILE>)
> > {

> > .
> > .
> > }

> > I need to substitute a string in that array, such string like " > " to be
> > substituted with " &gt; ". Tried to look in newsgroups and online without
> > good help.

> > Thanks for any help

> > Sam

> open(INFILE, '<test.txt') || die "Error opening file.$!\n";
> while (<INFILE>)
> {

>   $_ =~ s/\</\&g\t;/g;
> }

your replacement regex does not affect the array at all! plus, i doubt you
tested the code, since the replacement expression does not perform the action
requested. it appears that you escape characters arbitrarily... none of the
chars you escaped have special meaning warranting escape in this situation.
--
Michael Budash
Michael Budash Consulting



Wed, 11 Aug 2004 17:41:52 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. split and substitute, substitute, substitute

2. Substituting many strings in one pass over the input

3. Substituting many sparsely distrbuted strings in many files

4. substituting strings in source trees?

5. Pattern Match - substitute a string after the match

6. Repeat string matching/substituting ?

7. substituting string of text

8. how to substitute letters within a string

9. Substitute _last_ occurrece in string?

10. substituting keywords in a string

11. how to substitute strings in a newly created file

12. substituting string with current date

 

 
Powered by phpBB® Forum Software