Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Andybo - How do I make the info $My_string

Andybo - How do I make the info $My_string

Andybo - How do I make the info $My_string

I see what you mean with the split function but how can I pick out this line from a file with various information (no lines the same as this one though) and make it $My_string??

Thanks Andy

Gordon Bell

RE: Andybo - How do I make the info $My_string

OK, say you've got a file like:

line 1
line 2
some data ¦ 100ns ¦ 200 ns ¦ 1
line 3
line 4

You only want to select lines that contain the data.  I'm going to assume a couple of things... 1 - that the data file is small enough  to be comfortably held in memory, and 2 - that the data lines always have 3 "¦" characters, and that the 2nd and 3rd fields data ends with "ns".

The following should then work:


# Read the contents of the file into an array.
open(INPUT, "</path/to/data/file");
while (<INPUT>) {
    push(@File_Array, $_);

# Now use grep to look for lines that match our
# data line pattern.
# .* matches any character 0 or more times
# \s* matches any whitespace char 0 or more times
# \d+ns matches any string of digits immediately followed
#       by the chars "ns".
# \¦ match "¦" chars.
@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, @File_Array);

# @Data_Lines now holds an array of lines containing the data
foreach $Line ( @Data_Lines ) {
    @Items = split(/\¦/, $Line);

    Do processing on fields here...


I've done a quick test, and this seems to work OK, though you might need to tweak it a little for your exact requirements.

RE: Andybo - How do I make the info $My_string

You can simplify AndyBo's program a bit at the expense of debuggability:

while (<INPUT>) {
    push(@File_Array, $_);

collapses to

@File_Array = <INPUT>;

I use this regularly. If you are sure of your regular expression, you can even add in the next line. Instead of reading the file into an array, then greping the data lines out of the array, you can grep the input directly:

open(INPUT, "</path/to/data/file");
@File_Array = <INPUT>

@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, @File_Array);


open(INPUT, "</path/to/data/file");
@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, <INPUT>);

But as I said at the start, collapsing this much happens at the expense of debuggability. If I have problems with the script not picking up lines that it should then I expand back to a loop so I can see what is happening.

- Kai.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close