LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-09-2006, 07:08 PM   #1
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Rep: Reputation: 16
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.
 
Old 03-09-2006, 07:54 PM   #2
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
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.
 
Old 03-09-2006, 09:06 PM   #3
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
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 ####
 
Old 03-09-2006, 09:32 PM   #4
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
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?!?
 
Old 03-09-2006, 11:07 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
try to replace the if statements with somthing like this..

if ($item2 =~ /[^0]/)

or slightly more readable..
if(!($item2 =~ /0/))
 
Old 03-09-2006, 11:25 PM   #6
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
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 11:26 PM.
 
Old 03-10-2006, 12:02 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> 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.
 
Old 03-10-2006, 01:06 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
You are using the wrong operator:
!= for numbers
ne for strings

Try these links:
http://perldoc.perl.org/
http://perldoc.perl.org/perlop.html#...or%2c-equality
 
Old 03-13-2006, 05:08 PM   #9
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
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.
 
Old 03-15-2006, 03:40 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
have you looked at the perl cgi module?
 
Old 03-15-2006, 06:04 PM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
One trick to remember it, is that it's the opposite convention to shell operators ... sigh ... ie -ne for nums != for strings ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting PHP to print variable value on a webpage tangle Programming 9 02-07-2006 03:39 PM
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 03:49 PM
Tainted variable problem (PERL) rajatgarg Programming 1 07-29-2004 02:54 PM
Perl variable string search ugenn Programming 1 05-07-2004 09:19 PM
question about $_ (default variable) in Perl realos Programming 4 07-04-2003 06:09 PM


All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration