Trouble with Splitting in Perl
#1
Hi,

I've been going back to reading my notes on the Perl language back in college, and I, unbelievingly, have reached an impasse. It's just the this isn't quite the problem that I had last time. Let's get into it.

Here's the delimited file; the delimiter is the | character
Code:
Colombian|The finest Colombian beans, low flame roasted to yield a rich flavour.|7.99|The finest Colombian beans, low flame roasted to yield a rich flavour.In the cup, Colombian offers a rich mellow flavor, pleasant acidity and heady aroma. The flavor is consistent clean and balanced with a strong finish. Ideal as your everyday coffee, but particularly good as that morning eye opener.

Here's my code:
Code:
#!C:\strawberry\perl\bin\perl.exe
print "Content-type: text/html\n\n";

# Open the Coffee Descriptions file for output (coffee_info.txt)
open COFFEE, "<coffee_info.txt";

# Collect some information from the Coffee Descriptions and output it
while(<COFFEE>)    # While still able to collect info
{
    ($name, $short, $price, $long) = split(/|/,$_);
    print "<STRONG>$name</STRONG>: $short<BR>";
} # End collect info and output while

# Close the file
close COFFEE;

And here's the output, which isn't the intended output:
Code:
C: o

The ideal output that I can't manage was to print out the first two fields of each entry, so if you read the code, it would be like this per entry:
Code:
Colombian: The finest Colombian beans, low flame roasted to yield a rich flavour.

I'm not cheating by telling people to do my homework for me. I didn't know how I wasn't stuck on this the first time around, and I know should have paid some more attention. Like I said though, it didn't come around the first time.

Please, if you have a clue as to how I should be coding this, I would be interested in listening to it.

Thank you.

EDIT: Just so you know, I have been reading up on the split command, and it was no different from the notes I received in college. Maybe I should find a better e-book and not count on my prof's notes.
Reply
#2
Try:
($name, $short, $price, $long) = split /\|/;
('|' would be interpreted as logical OR and has to be escaped). Have also removed $_ as it would be default if not provided.

Also remember to add a 'die' to the open call.
open COFFEE, "<coffee.txt" or die "Unable to open input file:$!\n";

Hope it works now.

perldoc -f split
Reply
#3
http://www.comp.leeds.ac.uk/Perl/split.html

Why Pearl anyway?
Reply
#4
No particular reason, I was trying to revise my notes from college.

Why? What is it with Perl?

EDIT: This just in! The correction works! Thank you SectorVector. So let me get this straight: if I didn't use escapes, the | character would be mistaken as a logical OR, just as a & character would be an and, right?
Reply
#5
Yay!
Um. Yes and No. '&' is fine as it is. The first argument to split is a pattern. So '|' alone has special meaning.
You can't associate a semantic to '&' in a pattern. So it would be the literal '&'.

So, ($a,$b) = split /&/, "Hello&World";

would assign Hello to $a and World to $b

- man perlre should help with patterns.
- Mastering regular expression by Jeffrey Friedl is a good and complete reference book.

Programming Perl (3 authors) and Perl Cookbook by Tom C are more than enough.
Learning Perl by Randall Schwartz is a good beginners book too.

PS:I love perl bcos it is "idiomatic" and great for small/medium sized "tasks". TMTOWTDI.
Reply
#6
The purpose of what I'm doing is to review my college stuff, but thanks.

No more questions. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with Deep Learning Interview Questions Avantika_Sharmaa24 3 9,372 Jul 13, 2023, 12:50 pm
Last Post: gulshan212
  Perl vs. PHP RobertX 6 39,390 Dec 11, 2020, 04:52 am
Last Post: waregim



Users browsing this thread: 1 Guest(s)