1832902 Members
2445 Online
110048 Solutions
New Discussion

Simple perl question

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

Simple perl question

Hello

When using the "next if" loop in perl, what is the correct syntax for :-

next if ($x ne "some string")

Jeff
12 REPLIES 12
harry d brown jr
Honored Contributor

Re: Simple perl question

man perlsyn

next some boolean operation;
or
next;

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: Simple perl question

that is OK, though I myself prefer

$x eq "some string" or next;


Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rodney Hills
Honored Contributor

Re: Simple perl question

"if" in this case is using the modifier syntax which is availabe on all expressions. Example-

print "hello" if $x > 5;

"unless" is also a modifier-

print "hello" unless $x <= 5;

In your example, the parenthesis aren't required, and using the "unless" modifier you get-

next unless $x eq "some string";

HTH

-- Rod Hills
There be dragons...
Jeff Picton
Regular Advisor

Re: Simple perl question

Hi

Thanks for the replies. This is the full problem (I wish to print all the values of $last_field except the ones containing the string).

$last_field=pop(@array);
next unless ($last_field eq "some_string");

Jeff
harry d brown jr
Honored Contributor

Re: Simple perl question

printf("%s",$last_field) unless ($last_field eq "some_string");

live free or die
harry
Live Free or Die
Jeff Picton
Regular Advisor

Re: Simple perl question

I meant to mention that I was printing to file not screen
harry d brown jr
Honored Contributor

Re: Simple perl question

open(OUTPUTFILEPTR,"> /tmp/somefilename");

some loop:

printf(OUTPUTFILEPTR "%s",$last_field) unless ($last_field eq "some_string");


live free or die
harry
Live Free or Die
Jeff Picton
Regular Advisor

Re: Simple perl question

This should work, however it does not strip out the unwanted list of strings.

I think it may be the fact I used the pop function on an array.
harry d brown jr
Honored Contributor
Solution

Re: Simple perl question

something like this?

#!/usr/bin/perl
#
@arrSTUFF="";
while () {
push @arrSTUFF,$_;
}
foreach $last_field (@arrSTUFF) {
printf("%s\n",$last_field) unless ($last_field =~ "crazyman");
}

You can use "chomp" to remove "CR's" after the foreach line above:
chomp($last_field);

live free or die
harry
Live Free or Die
H.Merijn Brand (procura
Honored Contributor

Re: Simple perl question

Almost right. Running that with (the recommended) -w switch would yield a warning, because an array is not initialized with a string, but with a list.

Knowing that, the initialization isn't needed at all, and the chomp can be done in one go

=~ operator doesn't take " as delimiter without the m prefix

For automatically aliasses $_, and printf isn't needed for a single string. The added \n (newline can be shorthanded to the -l command line opting, reducing Harry's snippet to the syntacticcally valid:

#!/usr/bin/perl -wl
chomp (my @arr = );
for (@arr) {
print unless /crazyman/;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Gregory Fruth
Esteemed Contributor

Re: Simple perl question

Perl's "grep" function might do what you
want in one step:

@array2 = grep(/^some_string$/, @array);

The array @array2 will be the subset of
items from @array that are not "some_string".
The ^ and $ anchor the pattern so that you
match "some_string" exactly and do not match
"also_some_string" or "some_string_or_other".
R. Allan Hicks
Trusted Contributor

Re: Simple perl question

Following example from PERL Black Book by Steven Holzner page 213 published by Coriolis

To trap for division by zero:

@a(0 .. 20)
@b(-10 .. 10)

DIVISION: while(@a){
$a=pop @a;
$b=pop @b;

next DIVISION if ($b == 0);
print "$a / $b = ".$a/$b."\n";
}

when $b==0, it skips to the end of the loop.
"Only he who attempts the absurd is capable of achieving the impossible