Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract data from array based on range from a column 1

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I have an array (@data) that contains data like this:

10 /var/tmp/test
8 /tmp/fdfd.s
132 /tmp/test/test2/sdafsdfsd
22 /opt/test/blah.txt

I would like to extract ranges of data based on the numbers in the first column. For example, all strings that are between 0-20 and all string that are above 200.

Can anyone help?

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Many people can help. But its always good to see the code you have written up to this point. That way we can see specifically where you need help.

Basically you read the file line by line, extract the number from the beginning of the line and check it using a binary math operator:

<
>
<=
>=
==

You could maybe also use <=> and check the return value to see if the number is higher, lower, or the same.

Code:
for (1..3) {
   $n = $_ <=> 2;
   print "$n\n";
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You mean that the array elements contain those lines as space separated strings?
And what do you mean by 'extract': just run through the array and tell whether each element is in the range or not?
And as there are multiple ranges to test, how many at the most can they be and how would they be input to the code (keyboard, file, hard coded...).
Can you post the code you arrived at so far?

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Essentially, what kind of subroutine could I use to get all string where the first column (or beginning of the string) is above 254? See the last data structure.
-----------------------------------------------------------
# Open file & create new array
open (FILE, "/tmp/length.txt") or die "YIKES, error opening file $file: $!";
@data = <FILE>;
close (FILE) or die;

#sort all the data by descending number
foreach(sort by_string_descending @data) {
print;

# Sorting subroutine
sub by_string_descending
{
$b cmp $a;
}


#extract all data over 255 characters
foreach( ??sub?? @data) {
print;

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
If a string starts with a number, even it is preceded by zeroes or blanks, and even if it is not separated by a blank from the subsequent text, perl will nicely interpret the string as a number when involved in a numeric operation. So just do
Code:
for(@data){
  print if $_>254;
}

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
If "warnings" are turned on that can fill up any error log with warnings and slow down the execution of the script especially if its a big file. So use the "warnings" pragma and use the "no warnings" switch in that block of code. You could use a regexp to extract the digits:

Code:
for (@data) {
   if (/^(\d+)/) {
      print if ($1 > 254);
   }
}


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin and prex1 have pretty much spelled out your answer. Nevertheless, here it is a little more explicitly:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[maroon]LINE[/maroon][maroon]:[/maroon]
[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$line[/blue] = [blue]$_[/blue][red];[/red]
	
	[black][b]my[/b][/black] [red]([/red][blue]$num[/blue][red])[/red] = [red]([/red][blue]$line[/blue] =~ [red]m{[/red][purple]^([purple][b]\d[/b][/purple]+)[/purple][red]}[/red][red])[/red]
		? [blue]$1[/blue]
		: [url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]Line does not match pattern: [blue]$line[/blue][/purple][red]"[/red][red];[/red] [olive][b]next[/b][/olive] LINE[red]}[/red][red];[/red]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$num[/blue] > [fuchsia]0[/fuchsia] && [blue]$num[/blue] < [fuchsia]20[/fuchsia][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$line[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]10 /var/tmp/test[/teal]
[teal]8 /tmp/fdfd.s[/teal]
[teal]132 /tmp/test/test2/sdafsdfsd[/teal]
[teal]22 /opt/test/blah.txt[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

- Miller
 
Works great, thanks all!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Along the same lines as before. In perl is there any easy way to extract data based on preceeding characters (the equivalent of using sed or awk in a shell)? Specifically I want to extract the filename in these data strings:

11 ../WINDOWS/
17 ../WINDOWS/system
19 ../WINDOWS/odbc.ini
27 ../tmp/barg/ind/try/bla.txt

The filename will always be the last string after the "/" but MAY have blank spaces in it.

Thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
couple of ways:

Code:
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple][purple][b]\/[/b][/purple]([[purple][b]\w[/b][/purple]. -]+)$[/purple][red]/[/red][red])[/red] [red]{[/red]
      [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple](regexp) Line: [blue]$_[/blue] - filename = [blue]$1[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [red]}[/red]
   [black][b]print[/b][/black] [red]"[/red][purple](split) Line: [blue]$_[/blue] - filename =[/purple][red]"[/red],[red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\/[/b][/purple][/purple][red]/[/red][red])[/red][red])[/red][red][[/red]-[fuchsia]1[/fuchsia][red]][/red],[red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[teal]__DATA__[/teal]
[teal]11 ../WINDOWS/[/teal]
[teal]17 ../WINDOWS/system[/teal]
[teal]19 ../WINDOWS/odbc.ini[/teal]
[teal]27 ../tmp/barg/ind/try/bla.txt[/teal]

The regular expression will not return anything for the first line, but the split function will. Instead of asking for "easy" ways you should be asking for "good" ways.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec::Functions[/green] [red]qw([/red][purple]splitpath[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[maroon]LINE[/maroon][maroon]:[/maroon]
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]

	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$id[/blue], [blue]$fqfn[/blue][red])[/red] = [red]/[/red][purple]^([purple][b]\d[/b][/purple]+)[purple][b]\s[/b][/purple]+(.*)[/purple][red]/[/red]
		? [red]([/red][blue]$1[/blue], [blue]$2[/blue][red])[/red]
		: [url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]Pattern not found: '[blue]$_[/blue]'[/purple][red]"[/red][red];[/red] [olive][b]next[/b][/olive] LINE[red]}[/red][red];[/red]
		

	[black][b]my[/b][/black] [blue]$file[/blue] = [red]([/red][maroon]splitpath[/maroon][red]([/red][blue]$fqfn[/blue][red])[/red][red])[/red][red][[/red][fuchsia]2[/fuchsia][red]][/red][red];[/red]
	
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$file[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]11 ../WINDOWS/[/teal]
[teal]17 ../WINDOWS/system[/teal]
[teal]19 ../WINDOWS/odbc.ini[/teal]
[teal]27 ../tmp/barg/ind/try/bla.txt[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]File::Spec::Functions - portably perform operations on file names[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top