getting front and back location of an array 
Author Message
 getting front and back location of an array

Hello,
    I want to get the value of the front and the back of a location of an
array, then how can i do?


    if ($_ =/apple/)
    {
        #then i want to show the front of it:boy or dog
        #then i want to show the back of it:girl or bird
    }
Quote:
}

Thank you~


Mon, 17 May 2004 01:10:31 GMT  
 getting front and back location of an array

Quote:

> Hello,
>     I want to get the value of the front and the back of a location of an
> array, then how can i do?


>     if ($_ =/apple/)
>     {
>         #then i want to show the front of it:boy or dog
>         #then i want to show the back of it:girl or bird
>     }
> }

I think you mean that you want the elements before or after
the element that matches your regex. If so, here's one of
many ways. It's not very perlish, but it's easy to follow.

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



       if ( $field[$i] eq 'apple' ){
         $field[$i - 1] and print "Back  = $field[$i - 1]\n";
         $field[$i + 1] and print "Front = $field[$i + 1]\n";
       }
     }

Cheers,
Bernard



Sun, 16 May 2004 09:40:28 GMT  
 getting front and back location of an array
Just do:

 print "$field[0]" . "$field[5]\n";

Regards,
Jason Gray

"Just Another Perl Programmer"



Sun, 16 May 2004 09:41:34 GMT  
 getting front and back location of an array

Quote:

> Just do:


>  print "$field[0]" . "$field[5]\n";

If you're joking you should make this apparent since the OP (who
is not a native speaker of English) might not understand your
intent and actually use your "solution".

If you're not joking then please stop using "Just Another Perl
Programmer" as a tag line.

Cheers,
Bernard



Sun, 16 May 2004 09:46:44 GMT  
 getting front and back location of an array

Quote:

> > Just do:


> >  print "$field[0]" . "$field[5]\n";

> If you're joking you should make this apparent since the OP (who
> is not a native speaker of English) might not understand your
> intent and actually use your "solution".

> If you're not joking then please stop using "Just Another Perl
> Programmer" as a tag line.

> Cheers,
> Bernard

That was intended to be an offset of a joke. I can addin some code if you
would like.

Regards,
Jason



Sun, 16 May 2004 09:59:48 GMT  
 getting front and back location of an array

Quote:




>> > Just do:


>> >  print "$field[0]" . "$field[5]\n";

>> If you're joking you should make this apparent since the OP (who
>> is not a native speaker of English) might not understand your
>> intent and actually use your "solution".

>> If you're not joking then please stop using "Just Another Perl
>> Programmer" as a tag line.

> That was intended to be an offset of a joke. I can addin some code if you
> would like.

I don't really care whether you "addin" some code or not, but
if you are making a joke (especially a bad one) you should make
that clear. More so in this case since the OP might not get it
being both a newbie and a non-native speaker of English.

Cheers,
Bernard



Sun, 16 May 2004 10:25:27 GMT  
 getting front and back location of an array

Quote:

>                                            ...here's one of
> many ways. It's not very perlish, but it's easy to follow.

I think it can be made more Perlish.

Quote:

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

Isn't it time we went over to "use warnings" is clmp answers?

Quote:




   for my $i ( 0 .. $#field ) {

Quote:
>        if ( $field[$i] eq 'apple' ){
>          $field[$i - 1] and print "Back  = $field[$i - 1]\n";
>          $field[$i + 1] and print "Front = $field[$i + 1]\n";

This wraps round one end but not the other.  I think it should do
both or neither.  Futhermore it fails if one of the elements is
actually '0'.

      $i > 0       and print "Back  = $field[$i - 1]\n";
      $i < $#field and print "Front = $field[$i + 1]\n";

Actually you could say just '$i' rather than '$i > 0' here but that's
a small gain in speed at the expense of clarity.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Sun, 16 May 2004 21:22:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. HELP:Elements in an array have a space in front of them

2. Trouble getting a status back from SMTP

3. References and EasyTree, getting data back

4. Getting data back from another application

5. Getting Perl CGI Script To Get Me Back To Main Html Page

6. Getting it back OUT again!

7. Socket problem: sometimes getting garbage back on Solaris machine

8. Getting back successful matches using the g option

9. Getting back code instead of page

10. Getting system call data back into program?

11. getting back # of seconds

12. getting the default namespace back

 

 
Powered by phpBB® Forum Software