LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-30-2012, 03:11 AM   #1
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Rep: Reputation: 15
perl script cannot match the word with [ or ] character.


hi

i have written a perl script to do text extraction & post-processing (matching etc).

I encounter a problem where my perl script cannot match the word with [ or ] character.
eg:
bit[0]
bit[1] etc
xxxxx[120]

the extracted word can be with or without the [ or ] character.

my syntax for match word:
if( $temp_pin =~ m/^@pin[$i]$/ )
 
Old 01-30-2012, 03:20 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
escape the characters:

if( $temp_pin =~ m/^@pin\[$i\]$/ )
 
Old 01-30-2012, 07:24 PM   #3
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
@pin[$i] is an array, I am going to match the element in that array.
therefore, i dont thinkg "if( $temp_pin =~ m/^@pin\[$i\]$/ )" is a right way.
any opinion?

part of my code:
@split2 = split(" ", @file2[$line2]);
$temp_pin = shift(@split2)
if( $temp_pin =~ m/^@pin[$i]$/ )
 
Old 01-30-2012, 07:34 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Wow...
Code:
@split2 = split " ", $file2[$line2];
$temp_pin = shift @split2;

if ($temp_pin eq $pin[$i]) {
  ...
Or if you don't need @split2 later in your code, this can be shorten:
Code:
$temp_pin = (split " ", $file2[$line2])[0];

if ($temp_pin eq $pin[$i]) {
  ...
 
Old 01-30-2012, 09:08 PM   #5
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
thx for shorten the script. (i am still new to perl, i usually write perl in C-like language)

I am more concern how to handle the [] in my array element.
 
Old 01-31-2012, 04:31 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
When you use:
Code:
if( $temp_pin =~ m/^something$/ )
It is equivalent to:
Code:
if ( $temp_pin eq 'something' )
You said you want to match the "element in that array"
So I thought you wanted to match the element value
Is it correct ?
 
Old 02-01-2012, 12:10 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Consider this code snippet
Code:
$var1 = "bit[20]";
$arr1[0] = "bit[20]";
if ( $arr1[0] =~ /($var1)/ )
{
    print "regex match\n";
}
if( $arr1[0] eq $var1 )
{
    print "str match\n";
}
With '[]' as part of the value you are trying to match , only the string comparison works.
Without the '[]' both work.
Note that the string comparison works with or without '[]' in the compared values.

Your Perl syntax generally bothers me:
Code:
# this is a Perl array
@pin;   

# this is the 'ith' element of the array
# a single value is always a scalar ($var), even if its an array element
# or a hash key/val eg $hash{$key} = $val, where %hash is the hash.
$pin[$i];

# cast an array ref ($i) stored inside an arr element ( $arr2[$i] )to an array
@arr1 = @{$arr2[$i]}
Can you clarify your code/data ?
 
1 members found this post helpful.
Old 02-01-2012, 02:42 AM   #8
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
Code:
$var1 = "bit[20]";
$arr1[0] = "bit[20]";
if ( $arr1[0] =~ /($var1)/ )
{
    print "regex match\n";
}
if( $arr1[0] eq $var1 )
{
    print "str match\n";
}
now i know =~ & eq match are different.
 
Old 02-01-2012, 07:43 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yep. If comparing "whole" values, use

1. eq for strings
2. == for nums (ints )
for floats you need to worry about rounding etc
Code:
# equal(NUM1, NUM2, ACCURACY) : returns true if NUM1 and NUM2 are
# equal to ACCURACY number of decimal places

sub equal {
    my ($A, $B, $dp) = @_;

    return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
  }

3. only use regex if you want to match a (sub-)pattern rather than a complete value

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


Best practice is to start all Perl progs with

Code:
#!/usr/bin/perl -w
use strict;             # Enforce declarations
-w equiv to 'use warnings;'

You can also do
Code:
perl -wc prog.pl
which does a pre-flight check without executing the prog.
 
1 members found this post helpful.
Old 02-01-2012, 11:26 PM   #10
casualzone
Member
 
Registered: Jan 2010
Posts: 189

Original Poster
Rep: Reputation: 15
thx a lot.
it is helpful for me as a beginner
 
  


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
splitting a fil.e word by word. PERL casperdaghost Linux - Newbie 6 11-23-2010 05:45 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
LXer: Revamped Perl Script To More Evenly Distribute Number Pool Match Odds LXer Syndicated Linux News 0 09-17-2008 01:30 PM
regular expressions: How can I match something except a word? [KIA]aze Programming 6 08-01-2007 07:38 PM
Perl script assistance; paste word into external command bru Programming 3 02-21-2007 07:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:15 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
Open Source Consulting | Domain Registration