CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Selecting a specific element of an array using regex?

 



crabigailrose
Novice

Jun 30, 2009, 2:49 PM

Post #1 of 13 (9274 views)
Selecting a specific element of an array using regex? Can't Post

hi! I'm writing a program to scan for all wireless networks in range, create profiles for them, and automatically connect to the strongest network in range I have a stored profile for or the strongest unencrypted network. The info I need to get online is stored in a file data.txt with each profile seperated by a \x17. These profiles are read into an array @inputs. I am scanning for networks using iwlist scan (gentoo linux command) and reading the output from that into an array @profiles, with each network being one element. here's my problem-- I'm using a regex to find any network that appears in both @inputs and @profiles, now I need to figure out how to select that element of @inputs so I can read the data into iwconfig and get online. Here's my code


Code
sub sort_iwlist 
{
#gets output from iwlist, sends erros to /dev/null
open (SCAN, "/sbin/iwlist scan 2> /dev/null |");

#input seperator is 'Cell '
$/ = 'Cell ';

#reads output of iwlist into array @profiles
my @profiles = <SCAN>;
#removes the first element, (all interfaces except wlan0)
shift @profiles;

# for every profile of iwlist
foreach my $profile (@profiles)
{
my $essid;
#if iwlist says encryption key is on
if ($profile =~ m/Encryption key:on/g)
{
#finds the essid of each network in range
if ($profile =~ m/"(.*?)"/)
{
# for every profile in data.txt
foreach my $input (@inputs)
{
#splits each profile from data.txt into an array @lines of newlines
@lines = split (/\n/, $input);

#$essid is the networks essid
$essid = $lines[2];
}

if ($1 eq $essid)
{
##CONNECT TO THIS NETWORK##
print "$1\n";
}
}
}
}
}


before I used a regex there, i had the program print every network in data.txt in a numbered list, had the user enter the number that corresponds to what they want to connect to, and using

Code
 $inputs[$user_pick-1]

to select the profile i wanted to read from. Now I want it all to happen automatically...can I select that element of the array using a regex, or something?


crabigailrose
Novice

Jun 30, 2009, 2:54 PM

Post #2 of 13 (9272 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post

Oh, if it helps this is an example of one element of @profiles

Cell 02 - Address: 00:18:39:CF:8C:E5
ESSID:"George"
Mode:Master
Frequency:2.437 GHz (Channel 6)
Quality=10/70 Signal level=-85 dBm Noise level=-95 dBm
Encryption key:on
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
12 Mb/s; 48 Mb/s
Extra:bcn_int=100


and one from @inputs

Ryan's House
essid
none
key
XXXXXXXXXXXXXXXXXXXXXXXXXX
ap
00:11:50:14:65:E0


vikas.deep
User

Jul 2, 2009, 11:53 PM

Post #3 of 13 (9249 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post


Code
Unanswered questions are like "Thorn In Flesh", a bit of pain and does not looks nice to 

eye for whatever the reasons.



For a moment, forget everything else about networks and all that --

Put simply, you have two text files and you want to pull out comman elements base on user essid If so please check that split on @inputs (I suspect) there in lies the problem and use print statements wherever in doubt.

A advise for that input separator Remember to use "local" before $/ ALWAYS else the default "\n" is changed and it may lead to problems.


Code

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


crabigailrose
Novice

Jul 6, 2009, 9:25 AM

Post #4 of 13 (9220 views)
Re: [vikas.deep] Selecting a specific element of an array using regex? [In reply to] Can't Post

what is local? i've never heard of that...I am mostly teaching myself perl, so Im sure there are several things i've missed. $/ is only set to 'Cell ' in this particular scope...I only pasted the one subroutine I was having problems with. Unless it needs to be something specific it is set as '\n' in every other scope.

I think I might have solved my problem....I'm going to write a subroutine to return the profile I want to select and assign it as a variable, then call for that variable when I want to read from the profile. Now I'm off to write it! thanks!


vikas.deep
User

Jul 7, 2009, 12:38 AM

Post #5 of 13 (9205 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post


Quote
what is local? i've never heard of that..


This sarcasm was totally unnecessary and uncalled for. In a public forum you should not always expect exact responses to your questions and you should be tolerant enough for the rest or if you feel that a particular response is ludicrous you can send a private mail, to the person; this is a decent way to do it.
It seems you are young (and immature).


Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jul 7, 2009, 3:32 AM

Post #6 of 13 (9202 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post

 
Dear crabigailrose

To the best of my understanding; even as you think that my earlier post was laughable but I must tell you very honestly that when I spent my time on your post I found problem with the following in your code

Code
@lines = split (/\n/, $input)

and it was not doing what was expected.

In fact I was thinking not to split on "\n" and ---
but as you say you have solved your problem on your own let's just forget it all
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


crabigailrose
Novice

Jul 7, 2009, 8:16 AM

Post #7 of 13 (9195 views)
Re: [vikas.deep] Selecting a specific element of an array using regex? [In reply to] Can't Post

I am still having problems with that line


Code
@lines = split (/\n/, $input)


but I'm not sure how to do what I need to without using the split?

When I run it I get

Use of uninitialized value in string eq at ./connect.pl line 69, <SCAN> chunk 4.
Use of uninitialized value in string eq at ./connect.pl line 69, <SCAN> chunk 4.
Use of uninitialized value in string eq at ./connect.pl line 69, <SCAN> chunk 4.
Goose

I can't figure out why I keep getting the uninitialized values...its more or less doing what its supposed to by printing "Goose" which is the essid of the only network im in range of right now. Line 69 is


Code
 
if ($1 eq $essid)


You are right, vikas.deep its something to do with my split, $essid is the uninitalized value. I'm getting the error 3 times and have 3 profiles in @inputs, so its happening on every iteration of my foreach loop. However it is matching $1 and $essid, and printing the correct profile...why would $essid be uninitialized if its finding the right profile?

And I wasn't being sarcastic, sorry if it came off that way. I didn't understand your post and was asking you to clarify.


(This post was edited by crabigailrose on Jul 7, 2009, 9:39 AM)


KevinR
Veteran


Jul 7, 2009, 8:39 AM

Post #8 of 13 (9191 views)
Re: [vikas.deep] Selecting a specific element of an array using regex? [In reply to] Can't Post


In Reply To

Quote
what is local? i've never heard of that..


This sarcasm was totally unnecessary and uncalled for. In a public forum you should not always expect exact responses to your questions and you should be tolerant enough for the rest or if you feel that a particular response is ludicrous you can send a private mail, to the person; this is a decent way to do it.
It seems you are young (and immature).


Quote


It didn't seem sarcastic to me, seemed like a reasonable question.
-------------------------------------------------


vikas.deep
User

Jul 7, 2009, 9:50 PM

Post #9 of 13 (9176 views)
Re: [KevinR] Selecting a specific element of an array using regex? [In reply to] Can't Post

Dear Sir

I am sorry (if that was not a sarcasm) for that misunderstanding. Please go through the following even if it may seem a bit out of topic, for every one (I believe) is here to gain knowledge, share experiences and above all make friends in the spirit of common universal brotherhood.

You see english is not my first language and today Indians speak English only because we were ruled over by British for well over a century (just like contemporary Hindi is a mix of pure hindi and persian brought by mughals to this land and which also gave birth to a new language urdu) and leading social reformers and modern thinking Indians of that time demanded english education for Indian masses and of course after India attained her freedom the special efforts that democratically elected government has put in towards this end. So today we are very unique population where every one is at least bilingual speaking his native language (usually Hindi or some other regional language) and english.

Middle classes are in particular Hindi speaking and english reading people, for official purposes we speak and work in english but in day to day life we are hindi speaking people.

All this is necessary to say here because I am a serious learner, (using my real name and email id and location) and plan to gain as much as I can from here and so I plan to stay here for long.

Even though english has emerged a common link language world over but semantics may differ. Also culture and environment matter a lot.

In my place, a person who has written a few subroutines and he is saying that this is only a part of the script that means he has written a pretty long piece of Perl code and then he is asking for what is "my" or "local" before a variable declaration in response to a person who asks him to do so is looked very suspiciously

(and in fact a unhealthy, unpleasant slang is used for such an attempt that "he is trying to make you ***(C-word)")

that is why i thought that it was a sarcasm. (and anyway Yesterday was a rough day and you know that I was at receiving end in beginners section also)

I am happy that things have cleared over here. and Yes when I worked out that problem I clearly saw that there was a problem there in that split and now he is getting warnings because there is nothing to split there and in fact that split is useless beacuse he can and should directly use whatever he is getting from @inputs. I will again go through the whole thing to the best of my ability.

and yes within the confines of a subroutine and within {}; may be "local" might not be essential (I am not very confident about this) but I am in a habbit of invariably using local when ever I have to play with input separator. May be here I was wrong for suggesting local here but that was the first thing that struck my mind (Hey! why this man is not using "local" while playing with input separator).

I will go through the relevant documentation and get back on that

Quote
"local" Now that is "the" thorn in flesh here

-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


vikas.deep
User

Jul 7, 2009, 10:22 PM

Post #10 of 13 (9174 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post

Dear Crabi

"Use of uninitialized value in string " may just be warnings and if you do not use warnings in your code these messages should disappear. Here, of course you are having a "Goose" that you are able to print but in the sample that I tested there was no "Goose" and so I was not able to print anything.

Yesterday, was a bit rough day and I was angry with some things and also particularly with myself and got just a bit touchy but all that is past and today I will re-read the entire thing here and get back.

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"


crabigailrose
Novice

Jul 8, 2009, 12:19 PM

Post #11 of 13 (9167 views)
Re: [vikas.deep] Selecting a specific element of an array using regex? [In reply to] Can't Post


Quote
"Use of uninitialized value in string " may just be warnings and if you do not use warnings in your code these messages should disappear. Here, of course you are having a "Goose" that you are able to print but in the sample that I tested there was no "Goose" and so I was not able to print anything.


Thanks, I am using strict and warnings, so your right I guess that is why I'm getting those warnings. I just didn't know if it would mess up anything later in the code, or if I could ignore them or take off warnings.

This program won't run correctly unless you create a data.txt file in a specific format and use the iwlist scan command for linux, so it sorta depends on what OS you are using whether or not you get the intended results.

I got rid of the split, and now I'm trying to make


Code
sub select_profile 
{
#for every network that shows in iwlist scan
foreach my $profile (@profiles)
{
#searches for essid in output of iwlist scan (networks in range)
if ($profile =~ m/"(\w*?)"/g)
{
#$name is essid of the network
my $name = $1;

print $name;

#for every profile in data.txt
foreach my $input (@inputs)
{
#if profile contains the essid of a network in range
if ($input =~ m/$1/g)
{
#@matches contains all networks that show up in both iwlist scan and data.txt
push (@matches, $input);
}
}
}
else {print "NO MATCHES"};
}

#print @matches;
}


do what I need it to. However I can't seem to get the line

Code
	if ($profile =~ m/"(\w*?)"/g)

working correctly. It doesn't match anything, but it doesn't not match anything either so I'm not sure whats wrong


crabigailrose
Novice

Jul 8, 2009, 1:38 PM

Post #12 of 13 (9165 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post

I got it working just about perfectly, without using that split!


vikas.deep
User

Jul 9, 2009, 2:12 AM

Post #13 of 13 (9153 views)
Re: [crabigailrose] Selecting a specific element of an array using regex? [In reply to] Can't Post


Quote
I got it working just about perfectly, without using that split!


Hey Crabi,

Very good!!
but still
Please see if the following makes sense to you.

Code
#! /usr/bin/perl  
use strict;
use warnings;
& VIKAS;
open (my $FH, "text") or die "cannot open $!";
my $ star = <$FH>;
print $star,"\n";
sub VIKAS
{
$/ = 'JK';
}
# The output is as follows
#[root@localhost Documents]# perl hell.pl
#ABCDEFGHIJK


and now do the following

Code
#! /usr/bin/perl  
use strict;
use warnings;
& VIKAS;
open (my $FH, "text") or die "cannot open $!";
my $ star = <$FH>;
print $star,"\n";
sub VIKAS
{
local $/ = 'JK';
}
# The out put is as follows
#[root@localhost Documents]# perl hell.pl
ABCDEFGHIJKLMNOPQRSTUVWXYZ


The text is a file having the following

ABCDEFGHIJKLMNOPQRSTUVWXYZ

This was what I actually meant you might be thinking that you have set input separator to "CELL" only in that subroutine and rest of the script it is default "\n" but it is never so, even if in any one sub routine you do not use "local" it gets changed everywhere and any other subroutine using the same string will give you spurious results.

I thought a person who has written a perl code with few sub routines and especially involving input separator might be knowing this thing and he is simply trying to have fun and all that ----

As you have worked yourself a way out good but now I don't think you will ever have a problem with "local".
Also please see

Quote
Localization of special variables

If you localize a special variable, you'll be giving a new value to it, but its magic won't go away. That means that all side-effects related to this magic still work with the localized value.

This feature allows code like this to work :

# Read the whole contents of FILE in $slurp
{ local $/ = undef; $slurp = <FILE>; }

from http://perldoc.perl.org/perlsub.html#%26quot%3bPrivate-Variables-via-my()%26quot%3b

The exact line where I read (that one should always use local before $/ ;somehow I am unable to recall now ) but the meaning is clear--

Quote
-For all my suggestions " I am sure someone else can do it in a better or elegant manner!"

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives