LinuxQuestions.org
Help answer threads with 0 replies.
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 09-14-2004, 07:11 AM   #1
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Rep: Reputation: 15
extracting first n characters of a string in perl


hi everybody,
i am trying to write a perl script that extracts the first n characters (say 5) from 2 strings (both string s have more than n characters) and check whether they are same. i know how to take a string and check if it matches the first 5 characters of another string, but how how do i check for the first 5 chars of both strings?
 
Old 09-14-2004, 07:34 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
I would do :

Code:
$str1 = "test12";
$str2  = "test1234";

$five_first = $str1;
$five_first =~ s/(^.{5}).*$/$1/;

if( $str2 =~ /^$five_first/) {
    print "5 first chars match\n";
}
 
Old 09-14-2004, 07:51 AM   #3
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
that works perfectly! but help me understand this

Code:
$five_first =~ s/(^.{5}).*$/$1/;
the first five characters in 'five_first' are stored in $1 and the whole string $five_first is substituted with $1, right?
 
Old 09-14-2004, 07:57 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
A simpler / more readable soln would be to use substr(0):
$value = substr($string, $offset, $count);
eg
$value = substr($string, 0, 5);
you could even compare the 2 strings in one hit:
Code:
if( substr($string1, 0, 5) eq substr($string2, 0, 5) )
{
    do something...
}
 
Old 09-14-2004, 08:25 AM   #5
ananthbv
Member
 
Registered: Nov 2003
Posts: 49

Original Poster
Rep: Reputation: 15
this is great, thanks a lot chris and cedrik.
 
  


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
Extracting RAR - Japanese characters Danodare Linux - Software 1 07-14-2005 06:33 AM
grep problem in extracting special characters imppayel Programming 8 12-02-2004 08:28 AM
Handle escape characters in a string Helene Programming 7 05-02-2004 12:43 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 06:43 PM
extracting more than one value from a string ganninu Programming 16 12-10-2003 04:26 AM

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

All times are GMT -5. The time now is 11:07 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration