Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: one liner f...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 11043 of 11531
Post > Topic >>

Re: one liner for for and if

by rich.japh@[EMAIL PROTECTED] (Richard Lee) May 8, 2008 at 11:47 PM

Rob Dixon wrote:
> Richard Lee wrote:
>   
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> my @[EMAIL PROTECTED]
 = qw/one two three four/;
>>
>> print "$_\n" for @[EMAIL PROTECTED]
 last if "$_" eq q/three/;
>>
>>
>> [root@[EMAIL PROTECTED]
 tmp]# ./!$
>> ././././testthis2.pl
>> syntax error at ././././testthis2.pl line 8, near "@[EMAIL PROTECTED]
 last "
>> Execution of ././././testthis2.pl aborted due to compilation errors.
>>
>> Can someone fix my last statement on this program?
>>
>> I thought maybe this will work   but guess not... is there no easy way 
>> to do this?
>>
>> I don't want to do
>>
>> for (@[EMAIL PROTECTED]
) {
>>      if .......
>>      }
>> }
>>
>>
>> just trying to see what the correct format is that for one liner that I

>> am trying
>>     
>
> And you want to write it in one line why?
>
> print "$_\n" for grep /one/ .. /three/, @[EMAIL PROTECTED]
>
> But there's nothing wrong with
>
> foreach (@[EMAIL PROTECTED]
) {
>   print "$_\n";
>   last if /three/;
> }
>
> Rob
>   
#!/usr/bin/perl

use warnings;
use strict;

my @[EMAIL PROTECTED]
 = qw/one two three four/;

print "$_\n" for grep /three/, @[EMAIL PROTECTED]
 tmp]# !$
../test5.pl
three

this works well. However, that goes through entire array.. right?
So there is no true way to squeeze in the ' last ' .. ?

main reason is to see if it's doable(I like some of perl's idioms) and 
whether it is possible
The other reason is because I have a big array which I have to look in 
by 3 different steps
and I wanted to avoid 3 foreach loop in a row which key depends on 
preceding code and wanted to just go
---- simplied version----

my $something = $_ for @[EMAIL PROTECTED]
 last if "$_" eq q/three/;
my $something1 = $_ for @[EMAIL PROTECTED]
 last if "$_" eq q/$something/;
my $something2 = $_ for @[EMAIL PROTECTED]
 last if "$_" eq q/$something1/;

Anyway, I will try your code and re****t back.

thank you.
 




 7 Posts in Topic:
one liner for for and if
rich.japh@[EMAIL PROTECTE  2008-05-08 18:25:55 
Re: one liner for for and if
anexpert@[EMAIL PROTECTED  2008-05-08 21:37:11 
Re: one liner for for and if
Uri Guttman <uri@[EMAI  2008-05-09 03:20:26 
Re: one liner for for and if
rob.dixon@[EMAIL PROTECTE  2008-05-09 04:29:21 
Re: one liner for for and if
rob.dixon@[EMAIL PROTECTE  2008-05-09 04:26:53 
Re: one liner for for and if
Uri Guttman <uri@[EMAI  2008-05-09 03:56:57 
Re: one liner for for and if
rich.japh@[EMAIL PROTECTE  2008-05-08 23:47:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:21:10 CDT 2008.