what is "}{"?

Z

zenny lenny

I DID check the FAQ first.

Two questions:

1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'

What is the purpose of "}{"?

2. Is there any way to search groups or the web for symbols like "}
{"? Google seems to ignore symbols like this.
 
B

Brian McCauley

I DID check the FAQ first.

Two questions:

1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'

What is the purpose of "}{"?

It is the end of one block (executed once per line) and the start of
another block (executed once).

It makes sense if you know what -n actually does.

The above is simply short for:

perl -le ''LINE: while ( said:
2. Is there any way to search groups or the web for symbols like "}
{"?

Not that I know of.

However you could perhaps grep the Perl manual - this trick may be
mentioned in there somewhere.
 
K

Klaus

1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'
What is the purpose of "}{"?

You are using "-n" in a very clever way indeed:

perldoc perlrun:
....so with '$x+=$_}{print $x;'
your program will be...

LINE:
while (<>) {
$x+=$_}{print $x;
}

....or reformatted...

LINE:
while (<>) {
$x+=$_
}
{ print $x; }

-- Klaus
 
W

Wade Ward

Heh, that's intentional obfuscation. Abigail explains it
here (see the "Counting Lines" section):

http://ucan.foad.org/~abigail/Perl/Talks/Japhs/

The -n switch (see 'perldoc perlrun') wraps your
code, similar to -p but without the continue and
print portion.

You can also use a module to make things clearer:

$ perl -MO=Deparse -nle '$x+=$_}{print $x;'

BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
$x += $_;
}
{
print $x;
}
-e syntax OK
Aren't they just curly braces that are guaranteed to be unmatched? A google
search would hit every entry that has two or more sets of curly braces.
 
D

Dr.Ruud

zenny lenny schreef:
1. This code adds a column of numbers: perl -lne '$x+=$_}{print $x;'

What is the purpose of "}{"?

Try -MO=Deparse like this:

perl -MO=Deparse -ne'$x+=$_}{print $x'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top