Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
03-09-2006, 06:08 PM
#1
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Rep:
Perl wont print my Variable!!!
Hello guys,
Can any of you explain to me why perl won't print out any of the $item
variables after $item1 ?
But it DOES print out all of them if they are only numbers.
### START CODE ###
open(outfile,"<file.txt");
@data=<outfile>;
foreach $line (@data)
{($item1,$item2,$item3,$item4,$item5,$item6,$date,$name)=split(/:/,$line);
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item1</td></tr>";
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item2</td></tr>" unless $item2 == "";
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item3</td></tr>" unless $item3 == "";
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item4</td></tr>" unless $item4 == "";
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item5</td></tr>" unless $item5 == "";
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item6</td></tr>" unless $item6 == "";
}
close(outfile);
### END CODE ###
Thankyou.
03-09-2006, 06:54 PM
#2
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Original Poster
Rep:
Its weird because if I get rid of the <-- unless $item6 == ""; --> at the end of each line it will print out all the words as well as numbers...
But if one of $item() has no value then it prints out a blank line, which is not what I want.
So I need to be able to print out the html only if the $item() variable contains some characters.
03-09-2006, 08:06 PM
#3
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Original Poster
Rep:
I have now updated the code to look like this.
But it still doesn't print the variables.
Funnily enough though if i change 'if ($item2 != "0")' to 'if ($item2 != "1")' it will print the variable (and the blank lines for empty variables).
So it seems as though it assigns the number '0' to my variables......why??
#### START CODE ####
open(outfile,"<agenda_list.txt");
@data=<outfile>;
foreach $line (@data)
{($item1,$item2,$item3,$item4,$item5,$item6,$date,$name)=split(/:/,$line);
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item1</td></tr>";
if ($item2 != "0")
{
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item2</td></tr>";
}
if ($item3 != 0)
{
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item3</td></tr>";
}
if ($item4 != 0)
{
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item4</td></tr>";
}
if ($item5 != 0)
{
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item5</td></tr>";
}
if ($item6 != 0)
{
print "<tr><td style='border: solid 1px #EAEAEA; font-size:10px; '>$date</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$name</td>
<td style='border: solid 1px #EAEAEA; font-size:10px; '>$item6</td></tr>";
}
}
close(outfile);
#### END CODE ####
03-09-2006, 08:32 PM
#4
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Original Poster
Rep:
I have now discovered that by putting a number in front of a string of letters it WILL print that variable.
for example if $itemX contains the characters "1test" then it WILL print.
BUT if $itemX contains the characters "test1" then it WONT print.
What on EARTH is going on here?!?
03-09-2006, 10:07 PM
#5
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
try to replace the if statements with somthing like this..
if ($item2 =~ /[^0]/)
or slightly more readable..
if(!($item2 =~ /0/))
03-09-2006, 10:25 PM
#6
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Original Poster
Rep:
Thankyou very much for your reply Xhi.
Your first example seems to work exactly how I need it to - THANKYOU!
But the second example still prints out empty variables to the html page.
I don't quite understand how this code works...does it basicly say:
if item2 does not contain '0' then print
or am I off the track?
If you have any good Perl resources for me to follow in this kind of depth it would be much appreciated.
Once again, thankyou VERY much for your reply and the solution.
P.S do you know why my solution wasn't working?
Last edited by socceroos; 03-09-2006 at 10:26 PM .
03-09-2006, 11:02 PM
#7
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
> But the second example still prints out empty variables to the html page.
yeh on second thought that is not a good example here.. it may possibly work if you did
if(!($item =~ /^0$/))
though there should be a prettier way but i cant think of it right now..
> don't quite understand how this code works...does it basicly say:
> if item2 does not contain '0' then print
yes the [^0] says that we do not want to match 0.. so if there is anything other than 0 it returns success..
> P.S do you know why my solution wasn't working?
comparing strings with operators does not work.. or is not always defined.. my perl is rusty at best.. i just always use matching to compare strings..
> If you have any good Perl resources for me to follow in this kind of depth it would be much appreciated.
this matching that i did is using regexes (regular expressions).. essential to know if you are going to use perl.. regexes are a huge part of perl.. just search google for perl regex or regular expressions and the like you will find plenty of info..
good luck.
03-13-2006, 04:08 PM
#9
Member
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125
Original Poster
Rep:
lol, thanks for that one chrism01....
Now I feel really dumb.
Thankyou Xhi for your helpful reply's too.
I spent half a day on this one.
03-15-2006, 02:40 AM
#10
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
have you looked at the perl cgi module?
03-15-2006, 05:04 PM
#11
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740
One trick to remember it, is that it's the opposite convention to shell operators ... sigh ... ie -ne for nums != for strings ...
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 06:40 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News