Perl: Extracting a char from a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Extracting a char from a string.
# 1  
Old 07-31-2010
Perl: Extracting a char from a string.

I want to extract a character from a string.
For a C/C++ programmer like me, this would seem to be the most logical and reasonable way--but it doesn't work:
Code:
$s = "abcdefg";
$c = $s[2];

This way is documented and it works, but it seems clumsy.
Is there any simpler way to grab one character out of a string?
Code:
$s = "abcdefg";
$c = substr($s, 2, 1);

# 2  
Old 07-31-2010
In Perl, strings aren't character arrays like in C (C++ btw has a separate String class too) and can't be manipulated by character. One other way would be to use
Code:
$c=((split //, $s)[2]);

# 3  
Old 08-03-2010
Thanks, pludi. Though that one looks nearly as clumsy. And looking at it makes me fear perl would convert the whole string to an integer array before plucking one character out.

It's surprising that perl can be so remarkably terse in many ways, but clumsy in at least this one.
# 4  
Old 08-03-2010
are you sure you always want the character at second position in the string? a more perlish way is to write a regex and use the m operator to extract the character(s) you want. but yes depends on your requirement what exactly you are trying to achieve
# 5  
Old 08-03-2010
This is a representative sample of what I'm doing:
Code:
    my $chars = "!#&'()*+.:=@[]^_`{|}~";
    for (my $i = 0;  $i < $char_count;  $i++) {
        printf("\"" . substr($chars,$i,1) . " some other text %d, %d\",\n", $a, $b);
        # ...
    }

# 6  
Old 08-03-2010
Ah, a nice example for TMTOWTDI:
Code:
my $chars = "!#&'()*+.:=@[]^_`{|}~";
foreach my $char ( split //, $chars ) {
    printf( "\"" . $char . " some other text %d, %d\",\n", $a, $b );
    # ...
}

This should do the same, without substr or other cruft.
# 7  
Old 08-03-2010
But $char_count is usually less than the number of chars in the string.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Cast from String to char

Hello, This is my code: i'd like to like to add getenv("MYLIB") in the first case of my buffer inside of '1' , should i do the cast ? and how please ? Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

2. Shell Programming and Scripting

Perl script - Help me extracting a string

I have input like this : TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 07-FEB-2012 04:19:45 Copyright (c) 1997, 2009, Oracle. All rights reserved. Used parameter files: /t3local_apps/apps/oracle/product/11.2.0/network/admin/sqlnet.ora Used TNSNAMES adapter to resolve... (3 Replies)
Discussion started by: dnam9917
3 Replies

3. Programming

PERL \c char in the string

Hi guys, I am stuck up in a situation. I have a SUN box with certain logs which I need to parse to draw a report using Perl. Now, when I load the text file using a perl degugger to see how the text looks like when the first line of the log file is read in a variable. below is the snapshot of... (2 Replies)
Discussion started by: Asteroid
2 Replies

4. Shell Programming and Scripting

How to loop through every char in a string

for example this string: gLZMQp8i Loop become easy if we add space between each char, How to do it? or other solutions are welcome. (9 Replies)
Discussion started by: honglus
9 Replies

5. Shell Programming and Scripting

Parsing char string

I am stumped! I need to parse an input parameter to a script that has the form '-Ort'. I basically need 'O', 'r' and 't', i.e. the individual characters in the string parsed. Since there are no delimiters, I don't know how awk could do this. Can someone tell how to do this, this should be a... (5 Replies)
Discussion started by: ALTRUNVRSOFLN
5 Replies

6. Shell Programming and Scripting

last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also) each line of the file is a fullpath of a file. that makes the list huge. i need to add a functionalitie to that script, i have to be able to add /usr/* or /usr/ and with that reference all the files and folders... (6 Replies)
Discussion started by: broli
6 Replies

7. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

8. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

9. UNIX for Dummies Questions & Answers

string of 7 char length always...

Hi, I know, particular value in the variable should always be of lenth 7 , but the value that is present in thevariable might be of any no.of characters less than or equal to 7... if the no.of characters in the variable is less than 7, I want to add, zeroes at the starting of the field.. How can... (3 Replies)
Discussion started by: thanuman
3 Replies

10. Programming

Compare Char to String

This is actually a c++ question... Basically I am creating a program that asks for five characters. I have a dictionary file containing tons of words no long than five letters long, on a seperate line. I want to be able to take the five inputted letters and compare them to the words in the file... (3 Replies)
Discussion started by: Phobos
3 Replies
Login or Register to Ask a Question