[Solved]Need help splitting strings in Perl

Forum rules
Before you post please read how to get help

[Solved]Need help splitting strings in Perl

Postby RobertX on Sun Mar 30, 2014 8:55 am

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: Select all
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: Select all
#!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: Select all
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: Select all
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.
Last edited by RobertX on Tue Apr 22, 2014 8:38 pm, edited 1 time in total.
RobertX
Level 1
Level 1
 
Posts: 29
Joined: Thu Apr 12, 2012 6:09 pm

Linux Mint is funded by ads and donations.
 

Re: Need help splitting strings in Perl

Postby bjornmu on Tue Apr 01, 2014 1:43 pm

First, you Perl script is written for Windows as it refers to the Strawberry Perl installation path under C: :lol: The first line would normally be

#!/usr/bin/perl

| is a special character and needs to be escaped: split(/\|/,$_);

Personally I would prefer to write it as a normal string: split('\|',$_);
User avatar
bjornmu
Level 3
Level 3
 
Posts: 160
Joined: Wed Dec 19, 2012 2:50 am
Location: Trondheim, Norway

Re: Need help splitting strings in Perl

Postby RobertX on Tue Apr 08, 2014 1:07 am

Oops, sorry. I should have covered that up.

I do use Linux Mint 13 side by side with Windows 7.

Can't do anything without either one.
RobertX
Level 1
Level 1
 
Posts: 29
Joined: Thu Apr 12, 2012 6:09 pm

Re: Need help splitting strings in Perl

Postby pgmer6809 on Sun Apr 20, 2014 2:20 am

In addition to escaping the "pipe" character, I think your error may be in your print statement.
In Linux/Unix anyway you should end your print statements with "\n" to force a new line, otherwise all the lines overwrite each other and you just see the last thing printed.

pgemr6809
Nope. No "\n" needed.
Just escaping the pipe char.
Without the escape the split seems to just take the first four characters in the line and use those as the fields.
With the escape it works as it should.
this file:[list=]kona|gold|19.99|the best alternative to coffee
Starbux|dark roast|15.99|the drug of choice[/list]

produces this output: (note output all on one line)
    <STRONG>kona</STRONG>: gold<BR> <STRONG>Starbux</STRONG>: dark roast<BR>
pgmer6809
Level 3
Level 3
 
Posts: 103
Joined: Sun Mar 04, 2012 9:06 pm

Re: [Solved]Need help splitting strings in Perl

Postby michael-j-lacey on Fri Jun 13, 2014 7:52 am

This topic is solved I know - but there are a couple of tricks I've found useful when debugging perl scripts

1 - don't use just one line of data
2 - include the sample data file in your perl script using the __END__ thingy and the DATA file handle
3 - print stuff out, make sure you have what you think you have in your variables
4 - include dummy data (first line of DATA below)
5 - always chomp

Code: Select all
#!/usr/bin/perl

$debug=1;

print "Content-type: text/html\n\n";

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

__END__
NAME|SHORT|PRICE|LONG
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.
English|The finest English beans, low flame roasted to yield a rich flavour.|8.99|The finest English beans, low flame roasted to yield a rich flavour.In the cup, English 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.

michael-j-lacey
Level 1
Level 1
 
Posts: 2
Joined: Fri Jun 13, 2014 7:37 am


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest