RE: howto: array index inside a foreach loop

2002-11-22 Thread Ken Lehman
yeah sorry, my problem is not quite as simple as the example. I just put
that in to try and illustrate my point. I'm not trying to do a straight
replace, I need to replace an element only if I find a certain string
somewhere else.

-Original Message-
From: david [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 22, 2002 4:12 PM
To: [EMAIL PROTECTED]
Subject: Re: howto: array index inside a foreach loop


Ken Lehman wrote:

> Say I have a foreach loop that I used to modify elements when they match a
> pattern that I am searching for, but there is one special case where I get
> a match and I need the index for that element. Can I get that index or do
> I have to go with a for() loop. If this can be done how would I use it?
> Thanks in advance
> Ken
> 
> 
> # something along these lines
> foreach $temp_string (@array_of_strings)
> {
>  if ($temp_string =~  /^CRASH/)
>  {
>$array_of_strings[$this_is_what_I_need - 1] = "LOOK OUT!";
>  }
> 
> 
> }
> 

sorry i might be missing your point but why not just:

#!/usr/bin/perl -w
use strict;

my @array_of_strings = qw(CRASH Windoes use linux);

s/^CRASH/LOOK OUT!/ for(@array_of_strings);

print join("\n",@array_of_strings),"\n";

__END__

prints:

LOOK OUT!
windoes
use
linux

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: howto: array index inside a foreach loop

2002-11-22 Thread david
Ken Lehman wrote:

> Say I have a foreach loop that I used to modify elements when they match a
> pattern that I am searching for, but there is one special case where I get
> a match and I need the index for that element. Can I get that index or do
> I have to go with a for() loop. If this can be done how would I use it?
> Thanks in advance
> Ken
> 
> 
> # something along these lines
> foreach $temp_string (@array_of_strings)
> {
>  if ($temp_string =~  /^CRASH/)
>  {
>$array_of_strings[$this_is_what_I_need - 1] = "LOOK OUT!";
>  }
> 
> 
> }
> 

sorry i might be missing your point but why not just:

#!/usr/bin/perl -w
use strict;

my @array_of_strings = qw(CRASH Windoes use linux);

s/^CRASH/LOOK OUT!/ for(@array_of_strings);

print join("\n",@array_of_strings),"\n";

__END__

prints:

LOOK OUT!
windoes
use
linux

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: howto: array index inside a foreach loop

2002-11-22 Thread Tanton Gibbs
You need to use a for loop for this.
- Original Message -
From: "Ken Lehman" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, November 22, 2002 3:42 PM
Subject: howto: array index inside a foreach loop


> Say I have a foreach loop that I used to modify elements when they match a
> pattern that I am searching for, but there is one special case where I get
a
> match and I need the index for that element. Can I get that index or do I
> have to go with a for() loop. If this can be done how would I use it?
> Thanks in advance
> Ken
>
>
> # something along these lines
> foreach $temp_string (@array_of_strings)
> {
>  if ($temp_string =~  /^CRASH/)
>  {
>$array_of_strings[$this_is_what_I_need - 1] = "LOOK OUT!";
>  }
>
>
> }
>
> --
--
> 
> The views and opinions expressed in this email message are the sender's
> own, and do not necessarily represent the views and opinions of Summit
> Systems Inc.
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




howto: array index inside a foreach loop

2002-11-22 Thread Ken Lehman
Say I have a foreach loop that I used to modify elements when they match a
pattern that I am searching for, but there is one special case where I get a
match and I need the index for that element. Can I get that index or do I
have to go with a for() loop. If this can be done how would I use it?
Thanks in advance
Ken


# something along these lines
foreach $temp_string (@array_of_strings)
{
 if ($temp_string =~  /^CRASH/)
 {
   $array_of_strings[$this_is_what_I_need - 1] = "LOOK OUT!";
 }


}



The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]