LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl one liner error (https://www.linuxquestions.org/questions/linux-newbie-8/perl-one-liner-error-898821/)

casperdaghost 08-22-2011 02:48 PM

perl one liner error
 
for some reason i can't get this to work

Code:

perl –e ‘$_ = “The quick brown fox”; print scalar(split),”\n”;’
I get this error every time
Code:

bash: syntax error near unexpected token `('

anomie 08-22-2011 02:58 PM

Starting with the bare basics:
Code:

$ perl -e '$_ = qq(The foxy fox) ; print'
What is it you're trying to do from there? Looks like you want to split the $_ scalar (into a list), then force it back into a scalar context, in order to print it..?

What gives? :)

AlucardZero 08-22-2011 03:00 PM

Stop using "smart" quotes and hyphens.

This works:
Code:

perl -e '$_ = "The quick brown fox"; print scalar(split),"\n";'
‘’ is completely different than two 's, and wrong
“” is completely different than two "s, and wrong
The "–" you have is actually not the same as a "-". It is an EN DASH (E2 80 93) rather than a hyphen (2D).

casperdaghost 08-22-2011 03:01 PM

does the split function default to a space when nothing is specified, and doesn't the scalar function return the number of fields in an array?

anomie 08-22-2011 03:03 PM

Good catch about the bad characters.

@casperdaghost: what editor are you using to write your Perl?

anomie 08-22-2011 03:07 PM

Quote:

Originally Posted by casperdaghost
does the split function default to a space when nothing is specified, and doesn't the scalar function return the number of fields in an array?

Technically, split() defaults to whitespace, not just "a space".

If all you want is a word count, how about:
Code:

$ perl -e '@sentence = qw(The foxy fox) ; print @sentence . "\n" ;'

casperdaghost 08-24-2011 12:16 PM

Hey guys - I picked up that one liner from a tutorial pasted on a word doc. I cut and pasted it into a terminal, and it would not work. It drove me crazy because I was pretty sure what the result would be, but it was erring out. Really, really astute observation AlucardZero - and a good lesson for me.


All times are GMT -5. The time now is 08:33 AM.