LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 02-06-2007, 03:33 PM   #1
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Rep: Reputation: 0
nested Perl index gives error


Hello,



I am using a line of perl code in a bash script to search for a substring in a string.

MYSUBSTRING="TOP"
# I tried this line:
# POSITION=$(perl -e "print index($FILE_ATTENTE,$MYSUBSTRING);")
# and I also tried:
# POSITION=`perl -e "print index(${FILE_ATTENTE},${MYSUBSTRING});"`
# but with both previous I get the same exact result as with:
POSITION=`perl -e 'print index('$MYSTRING','$MYSUBSTRING');'`
echo "<!-- "
echo $POSITION
echo " "
echo $MYSTRING
echo " -->"
if [ $POSITION -gt 0 ]
then
# ... additiona code
fi

It works fine, giving "-1" when not found and an integer if found. Normal result:
<!--
-1

SYSTEM.JMS.D..MP
-->

The script fails for two strings, however.

The problem strings as they show up are:

<!--


SYSTEM.JMS.D.0.MA
-->
<font color="red">SYSTEM.JMS.D.0.MA</font><br>
<!--


SYSTEM.JMS.D.0.MP
-->

I get no "-1" value. Plus, I get the following complaints:


Bareword found where operator expected at -e line 1, near "0.MA"
(Missing operator before MA?)
syntax error at -e line 1, near "0.MA"
Execution of -e aborted due to compilation errors.
./getGFAQueues.sh: line 114: [: -gt: unary operator expected
Bareword found where operator expected at -e line 1, near "0.MP"
(Missing operator before MP?)
syntax error at -e line 1, near "0.MP"
Execution of -e aborted due to compilation errors.
./getGFAQueues.sh: line 114: [: -gt: unary operator expected


Don't understand what is causing the error. How do I fix?

Thanks in advance!
 
Old 02-06-2007, 04:13 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Please post scripts and output in [code] tags to improve readability.

First thing: Use of ! in double quotes: bash will attempt to expand this to an event, so I would expect to a message something like:
Code:
bash: !--": event not found
Use single quotes to prevent this:
Code:
echo '<!--'

Second thing: what are the values of MYSTRING and MYSUBSTRING? After the perl line, it would help to add an echo of these values to be sure what is going on. For sure the problem is something to do with quotes. Here's how I'd do it in a way which reduces the mess of quotes:
Code:
MYSTRING="J. R. Bob Dobbs"
MYSUBSTRING="Bob"
POSITION=$(perl <<EOP
  print index("$MYSTRING", "$MYSUBSTRING") . "\n";
EOP)
echo "MYSTRING      = $MYSTRING"
echo "MYSUBSTRING   = $MYSUBSTRING"
echo "POSITION      = $POSITION"
See if that helps.
 
Old 02-07-2007, 09:58 AM   #3
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
perl fails on dot dot

Hello,

The perl index function returns a null or blank on the following string values:

SYSTEM.JMS.D..MA
SYSTEM.JMS.D..MP

I am guessing it must be the concatenation operator (dot). All three of the following give the same results:
POSITION=`perl -e "print index(${MYSTRING},${MYSUBSTRING});"`
POSITION=`perl -e 'print index('$MYSTRING','$MYSUBSTRING');'`
POSITION=$(perl -e "print index($MYSTRING,$MYSUBSTRING);")

I guess I am now looking for a Perl funtion to indicate I need to ignore dots, which will wrap the index function.

Can someone please confirm?

Thanks!
 
Old 02-07-2007, 11:06 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
what are the values of MYSTRING and MYSUBSTRING when this happens?
 
Old 02-07-2007, 11:11 AM   #5
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
String = System.jms.d..ma
Mysubstring = Top
 
Old 02-07-2007, 11:12 AM   #6
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
previous post is not displaying properly, everything is uppercase
 
  


Reply



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
How to change the default webpage configuration from index.php to index.htm, etc meema Linux - Server 4 08-22-2008 09:06 PM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 10:28 AM
Perl: printing array's index and value craig467 Programming 8 08-30-2006 03:50 PM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 05:15 PM
PHP Undefined index Error Gerardoj Programming 1 05-30-2004 03:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 PM.

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
Open Source Consulting | Domain Registration